The A-Z of FileMaker: V is for Variable

A variable is a named location in memory capable of storing a value. Depending on the type, a variable has a limited lifespan and accessibility. In programming, the accessibility of a variable is called its scope.

In FileMaker programming, there are three types of variables – global, local and Let. Unlike other programming environments, all FileMaker variables store text values. While the value itself may be a number or a date, it is important to understand that FileMaker treats all variables as text.

Global variables

Global variables are named with two $ signs. A developer may create a global variable called $$LastLayout which stores the name of the last layout visited by the user.

A global variable is accessible from within a user session for a file. This means that the value can be accessed from any layout, any window, during any script, using any mode, etc., from within the file. When the file is closed, the global variable is cleared and the value contained is lost.

The scope of a global variable means that it cannot be accessed from another FileMaker file. In addition, it cannot be accessed by another user of that file.

Due to their scope, global variables are sometimes called file variables.

Local variables

Local variables are named with a single $ sign. A developer may create a local variable called $counter to keep track of the number of times a loop has run.

A local variable is accessible from within the script in which it is defined. When the script ends, the local variable is cleared.

The scope of a local variable means that it cannot be accessed from another script. For example, if script A creates a local variable called $user and then calls script B, script B has no access to the $user variable.

Due to their scope, local variables are sometimes called script variables.

Let variables

Let variables are created within a Let statement when defining a calculation expression. While some FileMaker programmers define Let variables with a prefix such as @, it is not required – Let variables can be plain English words.

Let variables are limited in scope to within the calculation in which they are defined.

Creating, modifying and destroying variables

Global and local variables are usually created and modified with the Set Variable script step.  By definition, Let variables are created and modified within a Let statement in a calculation.

set variable script step
Set Variable script step options
Let calculation variables
Let calculation using variables
define any variable in a calculation

Global and local variables can be created and modified wherever a calculation is created. This can include web viewers, conditional formatting and visibility, and various calculated labels (tab names and button bar segment names). This can allow for some creative programming if you understand when these calculations are evaluated. 

In FileMaker programming, variables are created by assigning them a value. This is different to other programming environments where the variable is often created before assigning it a value.

Reassigning a variable value is done using the same method as creating the variable – using the same name. In cases like counters, the variable can be incremented using a value that references itself – $counter + 1.

Local and Let variables usually die a natural death when the script or calculation ends; global variables die when the file is closed. However, any variable can be explicitly destroyed by assigning it a null value. This is done in FileMaker using two double quote marks (“”).

Naming variables

FileMaker variables are not case sensitive – setting the value of $counter will reset the value of $Counter. However, global, local and Let variables can exist with the same name – you can have counter$counter and $$counter at the same time.

Although not advised, FileMaker variable names can contain spaces – they can be multi-word names. For readability, it is advised that variable names use either camel-case or underscores e.g. $myVariable, $my_variable.

Strange but true

Let variables can be used to redefine constants such as Pi and True. For example, if you redefine Pi using Pi = 4, any subsequent expression will use the variable rather than the function. Similarly, you can define True = 0 with some strange results!

Using variables

As stated in the introduction, variables are temporary storage locations for text. As such, variables are often created in a context in which the value is known and then used in a subsequent context where the value may no longer be accessible.

create related record

A classic case is the creation of a related record. The primary key of the parent record is stored in a variable before moving to the child record location. The new child record is created and the Set Field script step is used to populate the foreign key with the variable value:

create related record with variable

Simplify and improve efficiency

In a calculation Let statement, variables are often used to improve readability, reduce the length of expressions, and improve the calculation efficiency.  Consider the Let statement below:

Let calculation variables
Let calculation using variables

The variables, today and taskdate have been used to simplify their source and meaning. The proximity variable is used to calculate the number of days to the task date from today. Once this is done (once), it is used in the Case function over and over without needing to be recalculated. This is far more efficient that getting the current date and comparing it to the task due date each time it is needed in the Case function.

Pass values between scripts

While the scope of a local variable is limited to the script in which it is created, the value can be passed to a called script in Perform Script as a script parameter:

pass variable as parameter

In the called script, the script parameter could then be assigned to a variable for use in that script:

set script parameter as variable

Note that the $sortOrder variables exist independently in the parent and called scripts and can have different values. When the process returns to the parent script, it will resume with its own value of the variable that exists there.

However, if the variable value has been modified in the called script in some useful way, and that new value is required by the calling script, the value can be passed back as a script result using the Exit Script step.

exit script with variable

In the parent script, the value passed back can be retrieved using the Get ( ScriptResult) function.  In the following script code the script result is used to redefine the $sortOrder variable in line 10.

refine variable with script result

Many FileMaker developers think that global variables are needed to be able to access stored values between scripts. The sequences above show the ability to pass a value to a script, have it processed in some way, and then passed back to the calling script. This is standard programming practice available in FileMaker scripting.

Leave a Reply

Your email address will not be published. Required fields are marked *