AnsweredAssumed Answered

OnRecordLoad can trigger unexpected results

Question asked by philmodjunk on Apr 22, 2009
Latest reply on Apr 24, 2009 by philmodjunk

Title

OnRecordLoad can trigger unexpected results

Post

This is not a complaint, nor is it a request for a new feature. Instead it is a cautionary tale that I am posting to this site to help other developers avoid falling into the same trap and to provide a work around for dealing with this situation.

 

I needed a script to do some housekeeping on the current record whenever the user used a specific layout to view/edit a record. Filemaker 10's new script trigger features seemed made to order for this.

 

So I created my script, entered layout mode and set the "OnRecordLoad" trigger for the layout. Then I tested my script by navigating to the layout. The script ran and ran and ran until I interrupted the script. I examined my script. There was a loop in the script, but the exit loop statement's logic was clearly correct. I couldn't see why the script never halted.

 

Fortunately, I am an FMP 10 adv user, so I enabled the debugger and data viewer and stepped through the script one step at a time. Everything seemed to work correctly until...

 

...I spotted the problem. In my script I switched to a different layout in order to process some related records and then switched back to my original layout. Switching back to the original layout triggers the OnRecordLoad script I had created to start performing all over again.

 

You may not realize this, but Filemaker scripts support recursive execution. That's programmer terminology for scripts that call themselves either directly or indirectly. Consider the following script named "Infinite Loop":

 

Set Field [Table::fieldA, Table::fieldA + 1]

Perform Script [Infinite Loop]

 

Even though there are no loop/end loop instructions, this script will add one to fieldA without stopping until you interrupt execution by pressing the appropriate key. You can halt your loop by adding steps that prevent the Perform Script step from making any more calls to perform the script:

 

Set Field [Table::fieldA, Table::fieldA + 1]

If [table::fieldA > 5]

    Exit Script []

End If

Perform Script [Infinite Loop]

 

The new tricky "Gotcha" to guard against with Script Triggers is now "Go To Layout" can act like a recursive Perform Script step to trigger additional calls of the same script.

 

This left me with a problem. Either I had to figure out a way for my script to do its job without changing layouts or I had to find a way to change layouts without getting trapped in this infinite recursion.

 

The above script example gave me the idea in how to modify my script and avoid the infinite loop. I added the following code to the beginning of my script:

 

If [$$RecordLoadCalled = "True" /* Avoid endless recursion */]

    Exit Script []

Else

     Set Variable [$$RecordLoadCalled, "True"]

End If

 

Then I added

 

Set Variable [$$RecordLoadCalled, ""]

 

to the end of my script and at any other points where I used Exit Script.

 

Now my script works.

Outcomes