Performing an illegal sort - no error message

I was helping someone debug a script that did not seem to be working. After much debugging, we finally identified that a Sort records script step wasn't working.

The table was almost (but not quite) sorted to begin with, so it looked like it was working poorly, when in fact it just wasn't working at all.

After a bunch more head-scratching, including looking for Unicode sort order issues, searching for invisible characters in fields, etc, we finally figured out the problem - this Sort step was running in Table A, but the Sort criteria were using fields from Table B.

Both table A and B have identically named fields, which made it less obvious.

Important note: Table A and B are not related, the fields are not Globals.

Why does FileMaker allow this?

  • At design time: The script editor doesn't know what context a step will occur in, so it's somewhat sensible that it can't prevent that mistake during script design.
  • At runtime: However, when the script step runs, the runtime absolutely knows that you can't sort Table A on fields from Table B, and it should throw up a runtime error. The fact that this isn't considered an error seems like an "interesting" choice, if not an outright bug.

Are you suggesting that we get a sensible error code?

1 Like

After the sort operation, Get ( SortState ) will say the records were sorted. Your sort was successful, a sort involving unrelated fields is simply neutral on the sort order. If you add other fields that match your context, the sort will work on those.

Basically FileMaker attempts to do many things wrapped into one. This is similar to importing records where some of the records get dropped because they fail some validation. In the case of the import you can get a dialog that gives you a recap of how many records were imported and how many were imported, but the step is still considered successful. Even if you wrap it in a transaction, the import succeeds and the transaction will fail.

Replace field contents where some records cannot be edited because another user holds a record lock on them falls in the same category. Your step was successful, even if not all records were edited as you intended.

Often times in FileMaker partial success equals success (when thinking of error codes). There is even some cases where we get error codes for operations that succeeds (like when using GTRR for the full record set, if your active record does not have a related record, GTRR succeeds, but reports an error number).

For steps that involve a single object, like setting an unrelated field, more often you will get an error code back.

You may wish FileMaker to be more strict about reporting operations involving objects that, at runtime, cannot possibly be involved. I am pretty sure others would like that too, myself included. Often times, we get silent failures. Even when we do get failures, a lot of developers are not strict about handling those (turn on 'Pause on error' and see what you get).

The code does what we tell it to do, it does not know about intent. I was talking to someone else recently who was sorting on a calculation field containing text but stored as number (the default type when you create a calculation field).

So the data threw you off. Having the TableOccurrence::Field in the sort dialog should have helped, but when we think things are set correctly at the script level, data (invisible characters) and indexing (unicode vs english) are good culprits to rule out. Assisting someone else in a debug session is also a very good thing: having an extra set of eyes is often a good shortcut, sometimes simply telling our problem to someone else helps in finding the issue ourselves (rubber ducking as it is called).

It may not help much, but if you want to be sure your sort order changed after a sort was applied, maybe you can test for GetRecordIDsFromFoundSet ( 4 ) before and after and compare that using the Exact( ) function. I sure don't feel like it could be needed everywhere, but for things where it can be critical, it is one test you can use.

Thank you for sharing, we can all learn from each other, specially when the code is not returning what we expect and that makes us trip.

1 Like

:backhand_index_pointing_up: