controller.find() fails

In the Servoy 3.5 developer reference guide, it says:

find will fail if autosave is disabled and there are unsaved records.

What if autosave is enabled? How come controller.find() still fails sometimes?

Hi Sammy,

Servoy might still have some data not saved. So when you use controller.find() you might want to use databaseManager.saveDate() just before that.
Come to think of it maybe Servoy should do that automatically since it’s a requirement that data is saved anyway…

Same goes for controller.sort() by the way.

Hope this helps.

Thanks, I’ll try that. However, it looks like databaseManager.saveData() could also fail. What would be the cause of that?

Hi Sammy,

sammyzheng:
Thanks, I’ll try that. However, it looks like databaseManager.saveData() could also fail. What would be the cause of that?

Field and table constraints can cause a saveData to fail. Like an invalid date in a date column, a text in a number field, no PK or a non-unique PK, etc.

controller.find() does a saveData() but if you have autosave off then you have to do it yourself because then controller.find() will not do it (then it works just like when a user tries to do auto save in the ui)
so with autosave off you have to do it yourself in the code so that there are no sudden surprises for you as a coder.

find() can fail because of a few things. If autosave is on then find tries to stop any edit this could fail because of validations that are failing and after that the save to the database could result in a sql error
then find will also return false.
in autosave off its a bit different because then it can return false in the scenario’s like it above but also when there are unsaved records in that current foundset you are trying to set in find.
Because if we would go into find those records could be lost and when then much later on a save happens and validation or save errors happens on those records you completely lost the ui connection.

Okay, it turns out that databaseManager.saveData() is failing too. We were able to replicate this problem by entering text into a number field, then hitting backspace to delete it, then entering a number into the field (but the text was still red indicating wrong data type).

But the problem seems to happen for us even when the users are not making such a mistake. Is there any way to find out if it is being caused by duplicate primary keys or null primary keys? Thanks!

We are still getting about 15 controller.find() failures every day, even with autoSave enabled. Anyone know how we can find out if it is caused by database constraints like null or duplicate primary keys?

Hi Sammy,

You might want to take a look at this threat where Hans Nieuwenhuis posted a method he uses to catch any saveData/commitTransaction failings.
Since a controller.find() needs/will try to save data first (depending on the autoSave state) you might be interested in this method.

Hope this helps.

Thanks, that was exactly the type of thing I was looking for. Unfortunately, databaseManager.getFailedRecords() always returns an empty array for us when controller.find() or databaseManager.saveData() has failed. What does that mean?

that could mean that somewhere you have a validation (ondatachange) method that says to the system that the current value that the user put in is incorrect
Then find and save wont happen. But you also wont have invalid rows because those are database related and not ui related (ui level validation)