Script Trigger pitfalls: Get(LastError)

Script Trigger Pitt Fall

Today I implemented some script triggers in an existing project and something strange happened. My navigation scripts that have worked correctly for years, suddenly stopped working. I checked my scripts over and over again and couldn't find any clue, until I discovered an unexpected behavior.

Let's simplify my problem. Start with a FileMaker file with 2 almost identical scripts:

Go To Layout 1
Set Error Capture [On]
Go to Layout ["Layout 1"]
If [Get(LastError) <> 0]
Show Custom Dialog ["Error"; "Error"]
Exit Script[]
End If
# Continue the script ...

Go To Layout 2
Set Error Capture [On]
Go to Layout ["Layout 2"]
If [Get(LastError) <> 0]
Show Custom Dialog ["Error"; "Error"]
Exit Script[]
End If
# Continue the script ...

Both layouts have the same script trigger defined for the event: "On Layout Load"

Set Error Capture [On]
Go to Object [Object Name: "FIELD"]

Layout 2 contains an object with the name "Field". On layout 1 this object is missing.
When running the script "Go to layout 2" from layout 2, the script will continue to run.
When running the script "Go to layout 1" from layout 2, the script will stop running and will show the error dialog.

I was puzzled and started debugging with the script debugger. The error returned from the Get(LastError) calculation was error 116: "Specified layout object is not present". This error cannot be caused by the previous script step "Go to Layout", but must be caused by the script step "Go to object" from the other script that triggered when loading the target layout.

Because this script has been fired between the "Go to layout" script step and the "If" script step, the error that has been captured is the error from the other script.

Conclusion: Get(LastError) can return an error code from another script.

Koen Van Hulle

Download the example file

One Response to “Script Trigger pitfalls: Get(LastError)”

  1. TomK Says:

    Hey, nice catch.

    Actually it seems a new behavior.

    Script control steps (if, else, else if, end if, loop, exit loop, end loop, exit script, and halt script) have always cleared LastError. This had made it difficult to both test Get(LastError) and then act on it afterwards because the condition used to test it would clear it.

    In FileMaker Pro 10, all script control steps never clear LastError any more. This means that it is now possible to have a script that tests Get(LastError) in an If statement and then use Get(LastError) in the following statement without having to save it in a variable first. This behavior is only available in this and newer versions of the FileMaker product family.

    http://tinyurl.com/of7s59

    KB Article : 7071

    Tom.

Leave a Reply

You must be logged in to post a comment.