loading and sorting in one step

Hi all,

I saw some code today that was trying to load records into a form using a relationship, and sort the records all in one step:

forms.theTargetForm.loadRecords(forms.utilityForm.utilityForm_to_TargetForm_relationship.sort('aField ascending'')

This is part of a larger method that creates a new record and performs a find on the utilityForm. What is odd is that starting up the client and running this routine the first time will not load the related records into theTargetForm. In the debugger I saw that forms.utilityForm.utilityForm_to_TargetForm_relationship.getSize() returned 3, but 42 records ended up getting loaded into theTargetForm. Running the routine again after this first failure works as expected. Taking out the sort from loadRecords step and sorting on a separate line causes the code to work as expected always, including the first time run after startup.

This is in servoy 4.1 I’d just like to know if sorting in the same line of code as loading the records is not recommended, and if so, why? What is it about this code that caused it to misbehave on the first run after a startup?

Thanks,
-Jeremy

Jeremy,

The sort method is a void method (returning nothing) which is what you are passing to loadRecords.
Doing this in 2 lines should work

forms.utilityForm.utilityForm_to_TargetForm_relationship.sort('aField ascending')
forms.theTargetForm.loadRecords(forms.utilityForm.utilityForm_to_TargetForm_relationship)

Note that in Servoy 4 introduced an ‘initial sort’ option in the relation definition, if you define your sorting there it will be applied each time the relation is loaded from the database.

Rob

Thanks Rob,

That makes sense, and is a good enough reason to always use 2 lines of code to load and sort. It’s what I usually do, I just came accross this code and was trying to figure out what was causing the results. It’s still weird that it fails on the first time after startup only, but who knows what else is going on in the environment to cause that. Your reasoning is good enough to never attempt to load and sort in one line. Thanks.

-Jeremy