Skip to main content

Hi,

I am creating scanner functionality for the mobile GUI which also must be available offline. I have the possibility to insert a new scan

Example of my insert.

var db = context.application.getStorageContainer();
var scan = context.application.model.getObj("scan");
var inserts = [];
var newRow = {
date_time : new Date()
,session_id : rowObject["session_id"]
,product_id : rowObject["product_id"]
,scanner_id : rowObject["scanner_id"]
,slaughter_sequence_no : rowObject["slaughter_sequence_no"]
,weight : rowObject["weight"]
,is_vib : rowObject["is_vib"]
,processed : 0
,deleted : 0
};

var insert = (function (insertRow){
// add record
return db.insertBookmark(scan, TSFRowBookmark.fromObject(scan, insertRow), true);
})(newRow);

This works great.

What I now need is the possibility to do a select from the scan table. (a count where session_id = @session_id). How can I achieve that?

Kind regards,

Peter

Hi @Peter Kruis,

Something like this should do the trick:

var query = new TSFQuery(scan.webserviceId);
var sessionIdFilter = {
session_id: newRow.session_id
};

query.keyRange = IndexedDb.createPKKeyRangeFromBookmark(scan, TSFPkBookmark.fromObject(scan, sessionIdFilter));
var deferred = jQuery.Deferred();
db.executeQuery(query, scan).always(function(reader) {
if (reader && reader.length) {
var row = reader.item(0);
// You have access to the 'row' variable here.
}
});

 

Kind regards,
Leroy Witteveen


Hi @Leroy Witteveen,

Most of it works great. However, when I set the rowObject value. It is not setted in the GUI when running it. However, when I am debugging it, it fills it. I think I am missing something small.

var db = context.application.getStorageContainer();
var scan = context.application.model.getObj("scan");


var query = new TSFQuery(scan.webserviceId);
var sessionIdFilter = {
scan_id: rowObject["scan_id"]
};

query.keyRange = IndexedDb.createPKKeyRangeFromBookmark(scan, TSFPkBookmark.fromObject(scan, sessionIdFilter));
var deferred = jQuery.Deferred();
db.executeQuery(query, scan).always(function(reader) {
if (reader && reader.length) {
var row = reader.item(0);
// You have access to the 'row' variable here.
rowObject["slaughter_sequence_no_previous"] = row.itemObject.slaughter_sequence_no;

//when debugging, above row is done (or at least the GUI will be filled). When normal running, the //GUI will not contain the slaughter_sequence_no.
//Using the same scan_id in both examples.

deferred.resolve();
}
});

return deferred.promise();

 


Hi @Peter Kruis,

You should see “db.executeQuery” as the equivalent to the SQL SELECT statement.
If you want to set “slaughter_sequence_no_previous” to the value of “slaughter_sequence_no”, you need the equivalent of the SQL UPDATE statement, which is db.updateRow.

It can be used like this:

var db = context.application.getStorageContainer();
var scan = context.application.model.getObj("scan");


var query = new TSFQuery(scan.webserviceId);
var sessionIdFilter = {
scan_id: rowObjecti"scan_id"]
};

query.keyRange = IndexedDb.createPKKeyRangeFromBookmark(scan, TSFPkBookmark.fromObject(scan, sessionIdFilter));
var deferred = jQuery.Deferred();
db.executeQuery(query, scan).always(function(reader) {
if (reader && reader.length) {
var row = reader.item(0);
row/"slaughter_sequence_no_previous"] = row.itemObject.slaughter_sequence_no;

db.updateRow(scan, TSFPkBookmark.fromObject(scan, sessionIdFilter), row, true).always(function() {
deferred.resolve();
});
}
});

return deferred.promise();



 


Good morning @Leroy Witteveen,

No, that is not really what I ment. I am executing this function from the default procedure. However, when executing, the fields in the screen (which are in the rowObject) are not changed on the GUI.

When I am debugging, they are. Looks like something is still executing parallel.

I will try to find it later this week.


Reply