Written byJonathan Davis
About the Author:Jonathan Davis is a Salesforce Consultant at Venn Technology in Grapevine, TX. He enjoys the challenge of mastering all things Salesforce. With seven certifications under his belt and counting, he’s always tackling Salesforce Trailhead to up his integration and configuration skills. |
In our previous article, we covered the Assignment, Decision, and Data Elements. These elements allow users to set values, branch the flow based on criteria, and interact with records in the database.
In this article, we will be talking about some common flow resources: Variables, Collections, and Formulas. These resources are created within the flow, and allow users to set and store values during the flow.
• Variables
• Collection Variables
• Record Variables
• Formulas
• Tips and Tricks
In flows, think of variables as a container to store a single value of a particular data type. They can start out as null (empty, or with no value at all), or with a default value. Variables can have their values referenced or modified during the flow.
Variables are created by clicking New Resource in the Toolbox section of the Flow Builder, and then completing the fields on the New Resource page:
The data type of the variable determines what values the variable can accept, and how you can interact with the variable elsewhere in the flow.
For example, two number variables with values of 1 and 2 could be added together in a flow for a result of 3. If the same variables were data type text, adding them together would result in a text string of 23.
Once set, the data type of a variable cannot be changed.
Variables can hold a single value of their type. They are used in Decision, Assignment, and Data Elements to check the current value, modify existing values, or set field values.
The two primary uses of the non record type variables are for storing input values from outside a flow for use inside it, and for creating internal values that can be set, referenced, and changed by the flow elements as the flow runs.
An example of the first use would be the below “NumberInputVar”—this variable has a data type of “Number,” and default value of “0” with “0” decimal places. It is also marked as "Available for input," which means that it can be populated from outside the flow before it runs by whatever triggered the flow. If no value is input, then it will default to 0.
An example of the second use would be the below “CounterVar”—this variable is almost the same as the previous NumberInputVar, but is not marked as “Available for input,” which means that its value always starts at 0 when the flow runs.
Variables like these can be used together to control the logic of flows. Shown below, a Decision element compares the NumberInputVar and CounterVar variables. If the NumberInputVar is higher, it executes an outcome.
The outcome leads to an assignment, which adds one to the CounterVar.
The path then leads back to the previous Decision element.
This has the effect of allowing the flow to accept a specific number and do something (in this case just adding to the number) that number of times.
To illustrate this in action, we will debug the flow. We are prompted to input a number for the NumberInputVar. In this example, we use the number 2. This number will not change during the flow but could be set to any number.
Reviewing the debug details will show that the flow moved through the Decision element three times, comparing the two variables each time.
The first time the CounterVar was its default value of “0,” so the Decision continued to the Assignment, which added “1” to CounterVar’s value, then led back to the Decision element. The second time the value of NumberInputVar (2) was still higher that the value of CounterVar (1), so the same result occurred, and the assignment added “1” to CounterVar, and led back to the Decision element. This time the value of NumberInputvar (2) was not higher than the value of CounterVar (2), so the flow took the default path and ended.
This is a simple example of how variables can be used to control flow logic based on inputs, but illustrates the utility and uses of variables in flows.
The NumberInputVar was set once outside of the flow and helped to determine the actions taken in the flow while the CounterVar always starts at “0” and helps to control the logic inside the flow as its value changes.
A variable with “Allow multiple values (collection)” selected is known as a Collection Variable. They do not have default values, but can be available for input or output. Collection Variables can be created like any other variable and are automatically created when a Get element is used to retrieve multiple records from the database.
Here, we are creating a collection variable of the Number data type:
A good way to think of Collection Variables is as a folder that holds an ordered list of one or more values that match its data type. The values inside the collection can be different from each other. For example, a collection of the Number data type might have the values 1, 47, and 586.20 inside of it, but it could not contain a value with the text data type such as the word “five.”
Record Variables are a special type of variable because they do not contain a single value in the same way as other variables, they contain an entire record and all of the field values on that record.
Record Variables can come from a few places.
For starters, you can create them like any other variable by selecting the Record data type, and the object you want the variable to be. When created this way, the variable has no default values, but can be populated with values during the flow. Here, we create an Account Record variable:
Alternatively, when you retrieve a single record with a Get element, the result is stored in a Record Variable. You can then reference and modify the values in that variable during the flow.
Finally, record variables are also generated automatically when you create a record triggered flow. In these flows, the $Record variable holds the current values of the record that triggered the flow. If a record triggered flow triggers on the update of a record (rather than just on its creation) there will also be a and the $Record__Prior variable that holds the values the record had before the update, which is useful for comparing the old values to the new ones in $Record.
For example, in this Decision element we compare the old value of an Account’s Owner ID field against the previous value to see if the owner was changed:
Record Variables can be referenced two ways. First, they can be referenced in their entirety, which is usually done when placing them into collections of similar Record Variables, or using them in a Create or Update element to create or update all of the values in the Record Variable.
Here, we use an Assignment to add an Account Record variable to a collection of Account variables:
Here, we use the whole Account Record variable in an Update element. This works equally as well with Record Collection variables:
You can also reference specific field values in Record Variables using something called dot notation, which references the record and a specific field connected by a period, such as “MyAccountVar.Id” to reference the Id field of an Account Record variable named MyAccountVar.
Here we set the value of a text variable AccountIDVar to equal just the Id of a record variable MyAccountVar:
Formulas are a resource in flows that are very similar to variables in that they have a data type and can be referenced by flow elements, but are different in that you cannot change their values using assignments. Instead, formulas output a value based on the logic you build inside the formula, and can themselves reference other variables in the flow.
If you are familiar with the formula fields used elsewhere in Salesforce then you will be right at home with using them in flows. The main difference between flow formulas and formula fields is that in flows the formula resources reference the current value of other variables in the flow, whereas formula fields reference the current value of other fields on or related to their object.
Both types of formulas change their values based on the values of the other values they reference, but because variables are able to change mid flow, flow formulas can feel much more dynamic.
In the below example, we create a formula resource of the Date data type that checks the Type field on the Account related to an Opportunity retrieved earlier in the flow with a Get element. If the Account’s Type is “Customer” the date value returned by the formula is 30 days from today’s (today being any date that the flow runs) date. Otherwise, the date value is 60 days from today’s date.
The CloseDateCalc formula resource can then be used in an Assignment element to set the value of the Opportunity’s Close Date field to the result of the formula:
Now that you know how to create Variables and Formulas, here are some tips and tricks on how to use them.
There are many times when you might want to do something on a flow only when a record is created, but do other things if it has only been updated. Depending on the situation, you could make two different flows for these situations (one that triggers on record creation, and another that triggers on record updates), but that can also take more time to build and maintain.
A useful option in these cases is to create a Formula resource of the Boolean data type with a formula that simply reads “ISNEW()” in the formula space. This formula will return a value of TRUE if the record that triggers the flow was just created, or FALSE if it was not. This Formula resource can then be used in Decision elements or other Formula resources as you move through the logic of your flow.
Text variables can be used to hold almost any collection of numbers, letters, and symbols, and can be modified during a flow to change and add to its value during a flow. For example, here we create a text variable with a default value of “Active Products: ” and then add the text value of a product name, followed by a comma and a space, to the variable with an Assignment element:
After the Assignment, the value of the ActiveProductList text variable would then be “Active Products: Automation Services, ” and we could repeat this step any number of times during the flow, adding any text that is needed to the ActiveProductList variable based on the flow logic until the list is complete. Once complete, a Formula resource can then be used to trim the extra comma and space from the end of the text value using the LEFT() and LEN() functions as shown here:
LEFT(value, number) returns a number of characters from the left of a text value, in this case our ActiveProductList variable. For the number, we want all but the last two characters, so we use LEN() to return the length of the ActiveProductList variable, and subtract 2 from that value.
This text manipulation can be configured to be quite complex, and can even be used to build HTML code that can start out as simple text values in text elements and formulas and then be displayed in email templates or on screenflow Screen elements to create formatted lists or tables.
For more information on Salesforce Flows, check out our next blog post: Flow Element Basics Pt. 3 - Loop, Collection Sort, and Collection Filters