FileMaker Sub-Scripts
One of the many features that make FileMaker such a powerful and flexible tool is the Script Workspace. Scripts make it possible for you to manipulate and execute various actions on the data within your application. They help to maintain efficiency, consistency and accuracy by automating manual workflows. Keep in mind, FileMaker 18 added 11 more file-based script steps to our tool belts.
We frequently use scripts to find and sort records, generate reports and send emails. We’ve even shared ways to move data from repeating fields and to manage and customize images in FileMaker with scripts. Depending on the task, a script could be as simple as a single line of code or as complex as hundreds of lines of code. Building a script can be a bit intimidating at first, but FileMaker’s low-code, step-by-step approach to building scripts makes the process pretty painless once you get the hang of it.
Please note, this article is intended for intermediate and advanced FileMaker developers. We assume you’re already familiar with FileMaker’s Script Workspace and you’ve written a script or two or three. If you’re completely new to FileMaker’s scripting environment, we provide an introduction to scripting workshop within our training curriculum at our San Francisco, New York, and Boston offices, or we can come to you.
INTRODUCING FILEMAKER SUB-SCRIPTS
Have you ever started building a script to automate a simple process and eventually you find that it keeps growing more and more complex? You have to add more functions as new requirements are uncovered and other user requests are submitted. What we end up with is a super long script with a bunch of If and Else If conditional statements to do this and that. We might even have a bunch of the same events triggered based on slightly different logical branching sequences. What’s more, we might realize that the names of our local variables are opaque because we’re afraid of interfering with other local variables somewhere else in the script. When we’re looking at an inordinately long and complicated script, it’s best to call in our trusty sidekick the sub-script.
First, let’s define what a sub-script is. If you browse through the list of available script steps in the right-hand pane of the Script Workspace in FileMaker, you aren’t going to find a step called Perform Sub-script, but you will find one called Perform Script or Perform Script on Server. When we use these script steps, we refer to them as sub-scripts. It tells FileMaker to pause the current script and run another script (the sub-script). When the sub-script is done, the app will resume the parent script right where it left off.
If you’ve specified actions inside of a parent script (this is the script which contains the “Perform Script” step in it) that needs to happen multiple times, either the same way or slightly different, it might be a sign that it’s time put those parts into a sub-script. Here’s an example:
You can put the duplicate steps in a sub-script and feed it some parameters so that it does its work slightly differently.
ADVANTAGES OF FILEMAKER SUB-SCRIPTS
Now that we’re on the same page when it comes to what a sub-script is, we’ll explore some of the benefits this technique offers us.
1. One advantage of organizing your scripts into sub-scripts is that it gives you the option to reuse local variable names without fear of poisoning the parent’s local variables. We know that each script is in charge of its local variables. And since the sub-script is running in its own protected space, you get a fresh start when it comes to naming local variables. Remember, if you want scripts that run in the same app file to share variables, you need to specify them as global variables by prefixing their names with double dollar signs ($$) as opposed to a single dollar sign ($) that we use for local variables. Here’s an example of two scripts with similarly named local variables.
Notice that the sub-script creates a variable in the parent script without any issues.
2. Sub-scripts are a useful organizational tool. They can help focus your mind on the task at hand. Let’s say you have a script that does dozens of complex things in a very specific order. If you’re like most developers, you can’t just sit down and create a complicated script from top to bottom within an hour. Advanced scripts generally require significant time commitments so it can be helpful to break it up into small, manageable chunks. We usually write a bit, test, edit, rinse and repeat until it works as expected then move on to the next part. You’ll also find that it’s easy to debug self-contained components of a script instead of the entire script.
Hopefully, this article sheds some light on how you might want to tackle and organize complex scripted workflows. We find that sub-scripts help increase our efficiency and productivity while reducing our stress levels. Be sure to check out our FileMaker script tips and best practices to keep your scripting frustrations to a minimum.