← servoy magazine

[Tips] Error checking find mode

  • Tips

This one recently caught us even though we've been aware of this case for a long time. It's pretty obscure so worth highlighting again.

When you script a find -- either using the controller or the foundset -- always check to see if find mode was entered before proceeding with your find logic. If you don't, you can inadvertently end up overwriting field values of the current record with your search criteria if entering find mode fails.

So instead of:

foundset.find()
foundset.id_page = obj.page.record.id_page
foundset.id_group = obj.group.id
foundset.id_version = obj.snapshot.id
var count = foundset.search()

Do this:

if (foundset.find()) {
	foundset.id_page = obj.page.record.id_page
	foundset.id_group = obj.group.id
	foundset.id_version = obj.snapshot.id
	var count = foundset.search()
}

controller.find() and foundset.find() return a boolean indicating if entering find mode was successful.

The question is: when does going into find mode fail? Basically anytime Servoy has unsaved changes. In our recent case it was probably bad data entry (entering alpha characters in a numeric field for example). To quote Paul Bakker, running "in 'autoSave = false' mode and there are unsaved changes" will also trip things up.

http://www.servoy.com/forum/viewtopic.php?t=10261#p51690

An extra note: according to Johann not using the error check when finding on foundsets derived from databaseManager.getFoundSet(...) is ok. 

Comments (3)

johan (single n! :)
"An extra note: according to Johann not using the error check when finding on foundsets derived from databaseManager.getFoundSet(...) is ok. " as long as you dont touch records of that foundset! so var fs = databaseManager.getFoundset() fs.find() // this one will work fs.search(); // search fs.getRecord(0).data = x; // alter data fs.find(); // now go again in find, this can fail!
David Workman
Thanks for ..both.. clarifications :) Apologies on the name, I should have that figured out by now.
David Workman
Another common reason when a find fails: auto save turned off either programmatically or at the server: databaseManager.setAutoSave(false)