Automatically Perform Script on Server

Automatically Perform Script on Server
By David Weiner

The ability of FileMaker Pro 13 and 14 to perform a script on the server instead of locally on the client's machine is a huge potential boon for productivity. Tasks for which a user might otherwise have to wait to complete can be accomplished in the background without holding them up. Performing certain scripted tasks on the server is also much, much faster in many cases. So, it's nice to perform a script on the server whenever you can.

Of course, you can't use "Perform Script on Server" (PSoS) if you don't have the following three conditions present:

  1. You have to be running a hosted FileMaker database
  2. The server that hosts it must support PSoS (v13 or v14)
  3. The script has to be compatible with PSoS (and set up to run that way)

While working on our second FileMaker module, fmRecentRecords, we perfected a neat scripting trick for automatically running a script on a server, whenever possible. The specific code is shown below, but it basically works this way:

First, the script needs to determine if the file is actually hosted (using Get (ConnectionState) ). Next, it needs to determine what version of FileMaker Server is hosting the file (using Get (HostApplicationVersion) ), and if that version is one that can handle PSoS (using PatternCount to look for the version numbers we require: 13 or 14). Once it's determined that the file is hosted by a version of FileMaker Server that can use PSoS, the script needs to know whether it is itself currently running on the local machine. If it is, then the script will recursevely call itself again using PSoS. Now the next time it runs (on the server), it will skip to the actual steps of the script we want to perform. If the file is not hosted, or is not hosted by a version 13 or higher server, then it will also skip the steps to call itself via PSoS, and the script just runs the rest of the procedure locally.

The beauty of this is that you can use PSoS when it's available, but still run just fine when it's not. Check out the code below:

 

 

Just insert some code like this into your script and you're off and running... on server!