The Support Group Blog

FileMaker Sub-scripts

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:

FileMaker sub-scriptsOriginal script with duplicate steps

You can put the duplicate steps in a sub-script and feed it some parameters so that it does its work slightly differently.

FileMaker sub-scriptsEdited parent script that calls sub-script

FileMaker sub-scriptsNew sub-script with single steps fed by parameters

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.

FileMaker sub-scripts

Parent scriptFileMaker sub-scripts

Sub-script

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.

FileMaker sub-scriptsComplex script

FileMaker sub-scripts

Script is broken up into three small, manageable scripts

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.

Find More FileMaker Tips & Tricks

 

This article is also published on FileMakerProGurus.com.

Share this entry
0 replies
BrowseMode

Sign up to receive news and information about the FileMaker platform and other custom app development tools.

Keep me posted

Most Popular

Developer Resources

News, Tips & Tricks and Demos