A variable is simply a named unit of data that is assigned a value. Its value can change, but its name will stay the same.
As detailed on the function call result page, many functions will create a variable at the end of their execution. For example, the Get answer of number field function stores the number it receives from the number component in a variable. You may need this variable as an argument in later function calls. Variables created by actions reside in the variable scope.
These variables can then be used in other function calls. They must be provided in the Arguments tab of an action. Click the input field to view all variables in the scope. An image of an example scope is shown at the bottom of this article. Make sure the variable you choose has the correct type. For example, if a function call requires you to pass a string, you must enter the name of a string variable.
Default variables in the scope
By default, the scope contains the following variables:
|CurrentUser.userId: Current user id||The id of the current user.|
|CurrentUser.firstName: Current user firstname||The first name of the current user.|
|CurrentUser.lastName: Current user lastName||The last name of the current user.|
These can only be used when a user is logged in. It this therefore recommended to only use them on authenticated pages or when necessary checks are in place.
The variable scope is best explained through an example. Let’s assume we are creating a form for users. At one point we ask the user to provide the number of pets they have. Evidently, we will represent this with a number field component. We want to add this number to a list.
We take the following steps:
- Create a new action for the number field component. This is explained in the Component actions article.
- Add a Get answer of a number field function call and select the number field component in the Arguments tab.
- The number entered by the user will be stored in a variable. We must name this variable in the Result tab. We have chosen Amount_of_pets. It is now part of the variable scope.
- Connect an Add to list function call to the one from from step 2. The result is shown in the image below.
- Select the list you want to add the number to.
- Choose the item (the number we just placed inside a variable) by clicking the Item input field with the scope option selected. Our number variable appears in the list. This is shown in the second image below.
- Finally, we must name the new list (with the pet’s age added to it) in the Result tab. Now we have created another variable, and the cycle continues.