Let
Purpose
Sets a variable or variables to the result of the specified expression(s) for the duration of the calculation, or until the script exits or the file is closed.
Format
Let({[}var1=expression1{;var2=expression2...]};calculation)
Parameters
var - any
variable name, local variable name, or global variable name (see
About naming fields for guidelines on naming variables).
expression - any calculation
expression,
field, or
constant.
calculation - any calculation expression, field, or constant.
Parameters in braces { } are optional.
Data type returned
text, number, date, time, timestamp, container
Originated in
FileMaker Pro 7.0
Description
The Let function allows you to assign the results of expressions to variables and return a result that can use those variables. Use Let to make complex formulas easier to read by reducing the need for calling functions within functions. Let can also make formulas more efficient by storing the result of function calls in variables for re-use within the formula, reducing the number of redundant function calls.
To assign multiple variables in one Let function, use a list syntax enclosed in brackets [ ] and separated by semicolons. To make multiple variables easier to read, you can put each variable and the returned calculation on separate lines, though this formatting is not required. For example:
Let ( [
variable = value;
variable2 = value2
];
calculation )
Three types of variables can be declared in a Let function:
•variable – a variable (with no prefix), which is available only within the function where it is declared
•$variable – a local variable (prefixed with $), which is available only with the script where it is declared
•$$variable – a global variable (prefixed with $$), which is available anywhere within the file where it is declared
Once defined, local and global variables can be referenced in any calculation within their scope. Local variables defined in a calculation are scoped to the file but are only available when scripts are not running. A local and a global variable (or even two local variables in different scripts) can have the same name but they are treated as different variables and store different values.
You can include an optional repetition number that appears in brackets [ ] immediately after the variable name. For example:
Let ( [
$variable[repetition] = value;
$$variable2 = value2
];
calculation )
The
Let function sets the variables from left to right. You can use previously defined variables (for example, variables that you defined with the
Set Variable script step) to define new variable values, and you can nest one
Let function within another. If you use a previously defined variable within a nested
Let function, the variable has scope only within the nested function (as if you had defined a completely unique variable). See the City example below.
Examples
Let ( x = 5; x*x ) returns 25.
Let ( [ x = 5; squared = x*x; cubed = squared*x ]; cubed ) returns 125.
The following example returns San Francisco - Paris.
Let (
City = “Paris”;
Let (
City = “San Francisco”;
City & “-“
)
& City )
The following example sets a local variable to the current account's privilege set and returns the contents of the variable. If this calculation is used in a script, the local variable would be available for the duration of the script.
Let ( $PRIVILEGE_SET = Get(AccountPrivilegeSetName) ; $PRIVILEGE_SET ) returns [Full Access] if it is evaluated by an account with the Full Access privilege set.
The following example sets a local variable counter at repetition 50 with a value of 120:
Let ( $counter[50] = 120; $counter[50]*2 ) returns 240.
The following example shows how to pass named parameters using the Evaluate, Let, and Get(ScriptParameter) functions, allowing access only to variable “a” (the example returns 6):
ScriptParameter = "a = 5; b = 10"
Evaluate (
"Let ( ["
& Get(ScriptParameter) & "
];
a+1 )"
)
The following example shows how to pass named parameters, allowing access to both variable “a” and variable “b”. The simplified first parameter makes the second parameter more complex (the example returns 6, 12):
ScriptParameter = "a = 5; b = 10"
Evaluate (
"Let ( ["
& Get(ScriptParameter) & "
];
a+1 & \", \" & b+2 )"
)
The following example shows how to pass named parameters while keeping the ability to check the syntax of the second parameter of the Let function (the example returns 6, 12):
ScriptParameter = "a = 5; b = 10"
Let ( [
a = Evaluate (
"Let ( ["
& Get(ScriptParameter) & "
];
a )"
),
b = Evaluate (
"Let ( ["
& Get(ScriptParameter) & "
];
b )"
)
];
a+1 & ", " & b+2 )
Related topics