FileMaker.PerformScript Javascript class extended
FileMaker.PerformScript Javascript class extended
4 June 2020 - Author : - Categories : Blog, FileMaker, Technique

FileMaker.PerformScript Javascript class extended

With the release of FileMaker 19, it becomes easier than ever (it was already largely possible before – I had the opportunity to show the “#hash trick” in a few conferences 😉 ) to make FileMaker and Javascript communicate. Notably by executing Javascript code in a Web Viewer thanks to the script step Perform Javascript in Web Viewer (FileMaker asking the Web Viewer to execute a Javascript), and to the FileMaker.PerformScript Javascript class (“FileMaker script name”, “Optional parameter” ), Javascript calling a FileMaker script.

However, in our opinion, the latter lacks some possibilities. We have mentioned here the impossibility to resume a FileMaker script that would be paused, but there is also the absence of possibility to differentiate the called script (callback) according to success or failure. This article develops this last point and provides a solution.

Synchronous programming

No library is necessary, but I had to write my own class that extends a bit FileMaker.PerformScript to FileMaker.PerformScript ( scriptName, parameter, successCallback, errorCallback ).

A small example to make a bidirectional interaction between FileMaker and Web Viewer. It is a system of callback and/or promise, a way that Javascript often calls something from the outside and depending on whether everything went well (success) or badly (error), you let something happen in your Javascript application.

In fact, it all boils down to this: “FileMaker, call this script. If it went well do this, if it went badly do that”.

A concrete example with calendar integration: if the user changes the location of an event, he must call FileMaker and save it in the database. If this was NOT successful (for example, a commit error), the transaction must be canceled and the event returned to its original location so that the user sees that it was not saved. This is called asynchronous programming, and it is something that you must absolutely understand if you want to continue with Javascript.

So in the calendar, here’s what really happens:

  1. The action in Javascript is called, with immediate actions: what to do if it goes well, what to do if it goes bad.
    Internally, these actions are recorded, and Javascript calls FileMaker.
    The Javascript code isn’t “running” anymore, there is no more pause, we have actually reached the end of the code.
    FileMaker does its job and decides if it wants to tell the Web Viewer if it went well or badly.
    The Web Viewer executes the pieces of code (saved in step 1).

In code :

#BUSINESS LOGIC
#add as much logic as you want, for instance try to write to a folder

#DEMO: a custom dialog
Show Custom Dialog [ Title: "Choice"; Message: "Was the record saved?"; Default Button: “Yes”, Commit: “No”; Button 2: “No”, Commit: “No” ]

If [ Get ( LastMessageChoice ) = 1 ]
    #we pass success to the callback
    Set Variable [ $result; Value:JSONSetElement ( "" ;
[ "id" ; JSONGetElement ( Get ( ScriptParameter ) ; "id" ) ; JSONString ]; [ "success" ; Get ( LastMessageChoice ) = 1 ; JSONString ]
)]
Else If [ Get ( LastMessageChoice ) = 2 ]
    #we pass error to the callback
    Set Variable [ $result; Value:JSONSetElement ( "" ;
[ "id" ; JSONGetElement ( Get ( ScriptParameter ) ; "id" ) ; JSONString ]; [ "error" ; Get ( LastMessageChoice ) = 2 ; JSONString ]
)]
End If

#RETURN TO WEB VIEWER
Perform JavaScript in Web Viewer [ Object Name: "wv"; Function Name: "fmcallback"; Parameter 1: $result ]

As you’ll see in the example, I’ve put a library in there. It took a lot of sweat and tears to make it all work. I would like to explain how it all works, but it boils down to this:

I’ve expanded the FileMaker.performScript a little bit so that you can now include a success callback and an error callback. I record it and then I call the FileMaker script. I adjust the parameter a bit, because I add another “id” and then the parameter as passed.
Think of it as a kind of “phone line that FileMaker can remember”. When FileMaker calls the web viewer again with this id, the web viewer knows what action it is.

In fact, the Web Viewer tells FileMaker, “Hello, I’m action 23 and I’d like you to save this record. Afterwards, FileMaker tells the Web Viewer that action 23 went very well!

And here is an example, play with it and if there are any questions, feel free to ask them below! It’s a rather difficult concept, but once you’ve mastered asynchronous programming, you’ll want to do the same in FileMaker 🙂

Promise

Oh yeah, for the diehards: it also works with promise (on Mac anyway, on Windows not sure), so you can do that too :

fm.performScript ( "getData", "" )
    .then ( (data) => {
       //data is script result of getData
       data.sort();
       fm.performScript ( "storeData", data);
     })
    .then ( ( data ) => {
       //data is script result of storeData
       //do things here
    })
    .catch ( ( error ) => {
        //this is the error of getData or storeData, cool isn't it? :)
        alert(error);
 
    })
Prev / Next Post
Comments (2)
  • Martin - 2 July 2023 - Reply

    Hi, just ‘one more thing’: you have a typo in the title: PerfomScript = PerformScript.
    Cheers!!
    Martin R.

    • Fabrice - 2 July 2023 - Reply

      Thank you! fixed!

Add comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.