Variable scoping: local vs global variables

Beginner

Understand how FileMaker scopes $local and $$global variables and avoid the pitfalls each carries in multi-user solutions.

What you'll learn

  • How $local variables are created, used, and automatically destroyed when a script ends
  • How $$global variables persist across script calls within a session
  • Why $$globals are safe in multi-user solutions (each session is isolated)
  • Common scope bugs and when to explicitly clear $$globals
  • How scope interacts with Perform Script and sub-scripts

FileMaker variables come in two scopes: $local (single dollar sign) exist only for the duration of the currently running script, while $$global (double dollar sign) persist for the entire session until the user closes the file or you explicitly clear them. Choosing the wrong scope is one of the most common sources of subtle bugs in multi-script solutions. In a multi-user environment, $$globals are per-session, not shared across users, which surprises many developers.

1/4
1

Using $local variables within a single script

A $local variable springs into existence the first time you assign it with Set Variable and disappears when the script exits. Sub-scripts called with Perform Script cannot see the parent script's $local variables -- each script has its own namespace. Use $local for any value that is only relevant to the current script's logic.

FileMaker Script
# Script: Process Invoice
Set Variable [ $invoiceTotal ; Value: Sum ( LineItems::Amount ) ]
Set Variable [ $taxRate ; Value: 0.08 ]
Set Variable [ $tax ; Value: $invoiceTotal * $taxRate ]
Set Field [ Invoices::TaxAmount ; $tax ]
# $invoiceTotal, $taxRate, $tax all vanish when this script ends

Sign in to track your progress and pick up where you left off.

Sign in to FM Dojo