Set Variable by Name
Sets a local or global variable with a calculated name to a specified value.
Options
-
Name is a text expression that specifies the name of the variable to create or update. The result must begin with $ for a local variable or $$ for a global variable.
-
Value is an expression for the value the variable is set to. You can enter text or specify a calculation.
-
Repetition is a numeric expression for the repetition (index) number of the variable. If you don't specify a repetition, the default is 1.
Compatibility
| Product | Supported |
| FileMaker Pro | Yes |
| FileMaker Go | Yes |
| FileMaker WebDirect | Yes |
| FileMaker Server | Yes |
| FileMaker Cloud | Yes |
| FileMaker Data API | Yes |
| Custom Web Publishing | Yes |
Originated in version
26.1
Description
This script step works like the Set Variable script step, except that the variable name is the result of a calculation. Use this script step when the name isn't known until the script runs—for example, when you build names from a counter or from data.
If the variable doesn't exist, this script step creates it. See Using variables.
Notes
-
This script step returns error 5 ("Command is invalid") and doesn't set a variable if Name is empty or doesn't begin with $ or $$. It returns the same error if no valid variable name follows the prefix.
-
Unlike Set Variable, this script step doesn't add a $ prefix to the name for you.
Example 1
Stores the number of found records in a global variable named for the table occurrence that the current layout is based on—for example, $$Customers_Count.
Go to Layout [ "Customers" ]
Perform Find [ Restore ]
Set Variable by Name [ "$$" & Get ( LayoutTableName ) & "_Count" ; Value: Get ( FoundCount ) ]
Example 2
Stores each line of a list in its own local variable by using a counter in the variable name. The dialog shows Red, Green, Blue.
Set Variable [ $list ; Value: "Red" & ¶ & "Green" & ¶ & "Blue" ]
Set Variable [ $i ; Value: 1 ]
Loop [ Flush: Always ]
Exit Loop If [ $i > ValueCount ( $list ) ]
Set Variable by Name [ "$color" & $i ; Value: GetValue ( $list ; $i ) ]
Set Variable [ $i ; Value: $i + 1 ]
End Loop
Show Custom Dialog [ $color1 & ", " & $color2 & ", " & $color3 ]
Example 3
Creates a local variable for each key in a JSON object. The variable name is the key, and the value is the key's value. After the loop, $name is Ana and $city is Oslo.
Set Variable [ $json ; Value: JSONSetElement ( "{}" ;
[ "name" ; "Ana" ; JSONString ] ;
[ "city" ; "Oslo" ; JSONString ] ) ]
Set Variable [ $keys ; Value: JSONListKeys ( $json ; "" ) ]
Set Variable [ $i ; Value: 1 ]
Loop [ Flush: Always ]
Exit Loop If [ $i > ValueCount ( $keys ) ]
Set Variable by Name [ "$" & GetValue ( $keys ; $i ) ; Value: JSONGetElement ( $json ; GetValue ( $keys ; $i ) ) ]
Set Variable [ $i ; Value: $i + 1 ]
End Loop