Create a Variable
After completing this unit, you’ll be able to:
- Create a flow variable.
- Define input and output variables.
Prepare to Create a Variable
Before you create your variable, figure out what kind of variable you need.
What type of data should the variable store?
First consider what type of data the variable will store, much like you do when you create a custom field.
|Data Type||Use to store...|
|Text||IDs, descriptions, and other text or long text data.|
|Record||Field values from a record, such as an opportunity. When this data type is selected, you create a record variable.|
|Number||A numeric value.|
|Currency||A currency value.|
|Boolean||A yes/no value, such as whether a checkbox was selected.|
|Date||A date value.|
|A date and time value.|
|A picklist value.|
|Multi-Select Picklist||Picklist values, separated by semicolons.|
|Apex-Defined||Field values from an Apex class. When this data type is selected, you create an Apex-defined variable.|
How many values should the variable store?
By default, variables store one value that’s compatible with the selected data type. If the data type is Number, the variable stores one numeric value. If the data type is Record, the variable stores field values for one record.
But what if you need to store multiple values in one variable, such as multiple email addresses? When you create a variable, you can enable it to do just that with the Allow multiple values checkbox. When that option is selected, you create a collection variable.
We don’t go into much detail about collection variables in this module, but you can see them in action in the Lightning Flow module.
Should the value be available from outside the flow?
Each variable in your flow gets its value from somewhere.
Some variables get their values from inside the flow, such as when the user enters something in a screen component or the flow looks up field values from a record.
Other variables get their values from outside of the flow—namely, from whatever started the flow. For example, if you distribute a flow on an Account record page, you can pass the account’s ID into the flow by using the flow’s input variables. An input variable is a variable that has the Available for input checkbox selected.
If whatever starts the flow should be able to set the value of a variable, make sure the variable is available for input. When in doubt, go ahead and make your variable available for input.
When you configure your variable, you see another checkbox: Available for output. When selected, you’ve created an output variable, which means the value is accessible to something outside the flow. Output variables are too advanced for this module, so we won’t go into much detail about them here. When in doubt, don’t make your variable available for output.
Where do you plan to use the variable?
If you already know where you plan to use the variable, review documentation to see whether that field requires a certain kind of variable.
For example, you need to automatically submit an opportunity for approval from a flow. Rather than using the default approval settings, you need the approval request to be sent to a specific user, so you need to set the Next Approver Ids field. When you look at the reference documentation for the Submit for Approval core action, you see that the Next Approver Ids field only accepts Text collection variables. So you need to store the ID of that user in a Text collection variable.
Create a Variable
- Open Flow Builder. From Setup enter Flows in the Quick Find box and click Flows.
- Click New Flow.
- Select Screen Flow and click Create.
- From the toolbox, click Manager.
- Click New Resource.
- For Resource Type, select Variable.
- Enter an API name and description for your variable.
- Select the appropriate data type.
- If you want to store multiple values (a collection), select Allow multiple values.
- For record variables, select the object whose record values you plan to store.
- Identify the variable’s availability outside the flow.
Store Information in a Flow Variable
Once you’ve created a variable, you’ve got a placeholder just waiting for a value to… well, hold. A variable is only as good as the values stored in it, and Flow Builder offers a few ways to populate that variable. Let’s review them.
|How to Populate a Variable||Description|
|Set the variable’s default value
||If you know what the initial value of the variable should be, set its default value when you create it. The variable keeps that value until it gets overwritten by another element.
|Get a value from a record and put that value in the variable
||When you configure a Get Records element (which we cover later in this module) or a record choice set, specify the fields to store and which variables to store them in.
|Change the variable’s value from an element
||The most obvious way to change a variable’s value is with an Assignment element. But other elements let you store values in variables. For example, the Create Records element lets you store the created record’s ID in a variable.
|Pass a value in from outside the flow
||Depending on how you distribute your flow, you can pass a value into an input variable. For example, if you add the flow to a Contact record page, you can pass the contact’s ID into a flow variable.