Reference > Functions reference > Logical functions > Let
 

Let

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 reuse 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 )

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 example 2, below.

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. See Using variables.

Example 1 

Let ( x = 5; x*x ) returns 25.

Let ( [ x = 5; squared = x*x; cubed = squared*x ]; cubed ) returns 125.

Example 2 

This example returns San Francisco - Paris.

Let (
City = "Paris";
Let (
City = "San Francisco";
City & " - "
)
& City )

Example 3 

This 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.

Example 4 

This example sets a local variable counter at repetition 50 with a value of 120:

Let ( $counter[50] = 120; $counter[50]*2 ) returns 240.

Example 5 

This 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 )"
)

Example 6 

This 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 )"
)

Example 7 

This 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 

Functions reference (category list)

Functions reference (alphabetical list)

About formulas

About functions

Defining calculation fields

Using operators in formulas

Using variables