By Daniel Wood, 16 December 2021
When developing and maintaining client solutions a really common task is to write a script to either add, modify or delete data for the user. This is usually a time consuming batch style operation that is often faster to run on the server rather than the client.
To run a script on FileMaker Server you have the option of either setting up a scheduled script via the admin console, or running it using the Perform Script on Server (PSOS) script step.
The latter option is the fastest option when needing to quickly run a one-off script.
There's always a catch. To run a script on server via PSOS, you first need to run a local script that can itself run the Perform Script on Server step to call your server-side script – what a drama!
For a long time I used a piece of code that I would always write out at the top of these scripts to achieve this:
Technically speaking there is nothing wrong with this code, I just got tired of writing this all the time. I wanted to abstract this out to a single reusable script. One that could be placed at the top of any other script to force that script to run on the server.
This is what I came up with:
Here we are passing the name of the script to execute on server as a parameter.
This helper script will terminate if already running on server (to prevent repeat recursive PSOS calls).
If it is being initiated by the client, it will perform script on server by name, using the name passed as a parameter.
The halt is to terminate the script that initiated the call. The initiating script is actually the one that is going to be run on server, so we don't need to also continue to run it on client.
Thus, in order to run a script on server, it just requires the below step as its first script step. The rest of the script can be written as if it is to be run on server.
A single step at the top of any script to have it run on server.
The PSOS helper is really optimised here to be run for one-off scripts. It is Asynchronous in that it does not wait for the currently running script.
To run this as part of a solution where certain scripts are run PSOS, then first thing you'll need to do is remove the Halt script step on line 23. This will allow further execution of other scripts to occur. If you wish this to be Synchronous, then change the PSOS call on line 22 to wait for completion. Usually a PSOS that waits for completion expects to get a result back, so in order to achieve this you can exit here and return Get ( ScriptResult ).
As always an example is included. This file doesn't really do much other than contain some. further information around the script, as well as the helper script itself so that you can put it in your own solutions.