Recursive scripts with a counter guard

Expert

Write self-calling FileMaker scripts for tree traversal and recursive data processing, protected by a depth counter to prevent infinite loops.

What you'll learn

  • How recursive scripts work in FileMaker (self-calling via Perform Script)
  • How to pass a depth counter through the script parameter
  • How to define and enforce a maximum recursion depth
  • A practical example: walking a parent-child hierarchy
  • How to accumulate a result across recursive calls

A recursive script calls itself via Perform Script, building up a call stack with each level until a base condition is met. FileMaker supports recursion, but has no built-in stack depth limit that surfaces cleanly to developers -- unguarded recursion causes FileMaker to crash or become unresponsive. A counter guard passed through the script parameter makes recursion safe by enforcing a maximum depth.

1/3
1

Design the base condition and depth guard

Every recursive script must have a clear base condition that stops the recursion when it is met. Add a depth counter to the JSON parameter, increment it at the start of each call, and exit immediately if it exceeds your maximum. A maximum of 50-100 is safe for most use cases; FileMaker's practical call stack limit is around 1,000 but hitting it causes a crash rather than a clean error.

FileMaker Script
# Script: Walk Category Tree
# Parameter: JSON with keys: "categoryID", "depth", "result"

Set Variable [ $params ; Value: Get ( ScriptParameter ) ]
Set Variable [ $categoryID ; Value: JSONGetElement ( $params ; "categoryID" ) ]
Set Variable [ $depth ; Value: JSONGetElement ( $params ; "depth" ) ]
Set Variable [ $result ; Value: JSONGetElement ( $params ; "result" ) ]

# Depth guard -- exit if we have gone too deep
If [ $depth > 50 ]
  Exit Script [ Text Result: $result ]
End If

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

Sign in to FM Dojo