Get ( LastError ) not functioning well

(Filemaker 19.5.4.401 on Mac OS M1)

I have here an example where Het( LastError ) is not functioning the way it should.

image

I want to check if the "Import Records"-step went ok but I get an error of the "Delete All Records"-script step: "Command is unavailiable..." because there was no record to delete.
Filemaker should reset this LastError before continuing with the next script step.

I see the error appear in the Script Debugger so I'm sure the error comes from the "Delete All Records"-step

Could you put the Set Variable to query the error after each line and put them into different variables, e.g. ErrorNo1, ErrorNo2, ErrorNo3, etc.

You should see it switch back to zero for successful steps.

Very good tip!
I made the test and it worked well.
But: after deleting the extra script lines, and getting back to the original situation, it also worked well !!??

It doesn't make me feel good, the idea that I can't trust the command Get ( LastError )
Thanks for you reply!

I wonder if it's more a display issue.
Please keep the Set Variable lines in.
They don't cost much and help debugging. And you may want to add some If there to handle the failures.

When it shows up at working speed (not step by step) then it is a malfunction in the trigger imo.
Anyway it is safer fm detects (wrongly) an error to much than one to few.
Thanks for helping me out!

When you wish to trap errors, do you insert

Set Error Capture [On]

? This script step suppresses normal alert messages displayed by FileMaker client. In a few places the alerts message is not suppressed (can't recall which one).

See Help file Set Error Capture | Claris Pro and FileMaker Pro Help Set Error Capture | Claris Pro and FileMaker Pro Help

The first paragraph under Notes is:

  • Use Get (LastError) immediately after the script step you intend to test; a successful intervening step may clear the error code you were intending to capture. See FileMaker error codes.

HTH

If the error only appears when running without debugger, adding 0.1s Pause steps sometimes help.

I do always use Set Error Capture ON when making scripts that will be used by other people.

And as you can see, the Get ( LastError) is immediately after te line I want to check.

Making a workaround is not the problem, the problem is my lack of trust now in the function Get ( LastError ) :face_with_raised_eyebrow:

1 Like

Have you try getting the last error right after Delete All Records to see if it catches the error ?

Right after Show All Records, you may call Get(FoundCount) and if there are no records skip Delete All Records.

Get(LastError) should be inserted after each script step, even if you do not want to check it. This way the error is reset for the next script step.

Another way to find where the error comes form is to not call Set Error Capture and Get(LastError) and to check in the debugger to stop after an error.

The question is: what does the get ( LastError ) reset?
Is it the first well executed command or is it a Get ( lastError ) command?
Obviously it should be the first. No one is entering a Get ( LastError ) script step after EVERY script step...

BTW, my problem was not finding a solution, which is very easy, but express the weird behaviour of and my lack of trust in the Get ( LastError ) script step.

Well, the problem may be that you run somewhere a calculation triggered by what you do and that may set a new last error state. And that may also be a plugin or a calculation triggered by hotkey, which evaluates something.

that is why we usually put the last error right in a variable after a script step.

Get( LastError ) returns the exit status of the last script step. This means it is extremely volatile, it isn't storing the last non-zero exit status. It is only storing the last exit status.

Set Field [ 'quantity' , 12 ]
If [ Let ( $error = Get ( LastError ) ; $error ) ]
    # only non-zero errors will trigger this block
    beep
End If

As I understand it, each script step sets an error result at the end of its execution (0 or other), which can be retrieved by a Get(Last Error) right after the script step.
This means that each script step overwrites the preceding step’s error result.

1 Like

Close!

Some script steps do not modify the Get (LastError) result, or at least not in the expected manner. Loop and End Loop, for example, never modify the Get (LastError) result. If itself never modifies Get (LastError) result. The calculation for the If script step, on the other hand, may modify the Get (LastError) result.

2 Likes

I feel like I'm gonna kick myself for not thinking of one, but what's an example of something you could put in an If[] calculation that would overwrite Get(LastError) ?

Per Claris documentation:

If you do not specify a calculation or if the calculation is unsuccessful, it will evaluate as false. Use the Get(LastError) function to capture these errors.

Claris documentation specifies the script steps that don't modify the Get (LastError) result:

The following control script steps do not clear the last error condition reported by FileMaker Pro: If, Else, Else If, End If, Loop, Exit Loop If, End Loop, Exit Script, and Halt Script.

3 Likes

What about Pause()?

Here is another tidbit from the same documentation:

If an error condition already exists when the user cancels an action, the existing error condition is not cleared.

1 Like

Pause modifies the last error condition.

1 Like