OnRecordEdit Trigger in FileMaker
At dotfmp conference someone mentioned that he is missing a trigger in FileMaker Pro for when the user starts editing the record and thus locks it. While FileMaker has trigger for record load, commit and revert, there is no edit one. You may do something with a Layout Keystroke, but not all editing is by keyboard usage.
We can implement such a trigger with MBS FileMaker Plugin, our schedule functions and FM.RunScript. When record loads, we start a timer to evaluate an expression every second. This calculation checks whether RecordOpenState changes to 2 for editing and then runs the script to trigger.
Our example uses a Status field in a Status table to show the status. This must be a field in another table to avoid our current record get state changed by our Set Field call. We start with the OnRecordLoad script to set our status and start the timer with Schedule.EvaluateAfterDelay. Delay is 1 second and repeat each second. The expression checks Get(RecordOpenState) and if it is 2, first stops the schedule and then runs a script in the current file named OnRecordEdit:
# OnRecordLoad in file OnRecordEdit trigger
If [ Get(RecordOpenState) = 1 ]
Set Field [ Status::Status ; "New" ]
Else
Set Field [ Status::Status ; "Loaded" ]
End If
#
Perform Script [ Specified: From list ; “Clear Timer” ; Parameter: ]
#
Set Variable [ $Expression ; Value: "If( Get ( RecordOpenState ) = 2; Let([r = MBS( \"Schedule.Release\"; $$timer ); $$timer = \"\"; r = MBS( \"FM.RunScript\"; Get(FileName); \"OnRecordEdit\" )];1))" ]
Set Variable [ $r ; Value: MBS( "Schedule.EvaluateAfterDelay"; 1; $Expression; ""; ""; 1 ) ]
In our OnRecordCommit trigger script, we clear the timer:
# OnRecordCommit in file OnRecordEdit trigger
Set Field [ Status::Status ; "Saved" ]
#
Perform Script [ Specified: From list ; “Clear Timer” ; Parameter: ]
In our OnRecordEdit trigger script, we show the user that editing started:
# OnRecordEdit in file OnRecordEdit trigger
Set Field [ Status::Status ; "Edit" ]
In our OnRecordRevert trigger script, we also stop timer:
# OnRecordRevert in file OnRecordEdit trigger
Set Field [ Status::Status ; "Revert" ]
#
Perform Script [ Specified: From list ; “Clear Timer” ; Parameter: ]
There may be more trigger, like mode switch, where you could clear the timer, but we expect those mode switches to also to also call Commit or Revert trigger.
To clear the timer, we have this script:
# Clear Timer in file OnRecordEdit trigger
If [ Length($$timer) > 0 ]
Set Variable [ $r ; Value: MBS( "Schedule.Release"; $$timer ) ]
Set Variable [ $$timer ; Value: "" ]
End If
Does this work for you?
Please let us know your experience and whether this is interesting for a production solution.
The example file will be included in next plugin release.