<span id="hs_cos_wrapper_name" class="hs_cos_wrapper hs_cos_wrapper_meta_field hs_cos_wrapper_type_text" style="" data-hs-cos-general-type="meta_field" data-hs-cos-type="text" >How to use variables in Salesforce Flow</span>
04/01/2021

How to use variables in Salesforce Flow

This post is part of a technical series to help Salesforce Admins automate business processes using Flow.

Variables let you store some data in one step of your Flow and then use it in a later step. Think of them like a custom field that's not attached to any particular object. Variables are one of the core building blocks of Flow, so in this post we'll learn how to create them, how to set their values and how to get their values later.

Create a variable

Flow considers variables to be a type of "Resource". To create a new Flow variable, click the "New Resource" button in the Toolbox on the left of the Flow Builder. In the "New Resource" dialog, select "Variable" for Resource Type".

Create a variable in Salesforce FlowJust like a custom field, you need to tell Salesforce some things when you first set it up:

  • API Name (Required) - This is just like the "Field Name" for a custom field. This is how you'll refer to the variable later on in the Flow. Make sure not to use spaces when choosing an API name. I like to use something simple and descriptive like "Account_Name" or "Incoming_Lead".
  • Description - This is optional, but it can be helpful to future Flow editors (or you) to make a note about what this variable is for.
  • Data Type (Required) - This tells Salesforce what kind of data you want to store in the variable. These are very close to the types you select from when creating a custom field. While these are almost the same as the data types you use for custom fields, they're not exactly the same. Let's dig in:
    • Text - This is any chunk of text you want to store. Unlike custom fields, you don't have to specify a specific length. You can store a lot or a little text in one of these.
    • Record - This is almost like a Lookup custom field. It lets you store a record so you can refer to it later. Because it's a record, if you select this, you'll also need to specify the Object you plan to store (e.g. Account, Lead, etc).
    • Number - When you specify a number, you're also asked to specify a number of decimal place. For whole numbers (e.g. 1, 2, 100, etc) you can specify 0 for decimal places. You can also specify a "default value" which is the value this variable will have if you haven't set it to anything else. Just like a custom field's default value.
    • Currency - Think of this as very similar to a number but it allows you to work with other data in Salesforce that deals with currencies.
    • Boolean - This is just a true or false value. It's like a Checkbox custom field.
    • Date - Use this to store a date (like April 4, 2021) when you don't need to store the time.
    • Date/Time - Use this to store a date and time (like April 4, 2021 at 2:30pm).
    • Picklist - This is an advanced topic that we won't cover in this post, but it allows you to use a Picklist to show choices.
    • Multi-Select Picklist - Same as Picklist above but for multi-select picklists.
    • Apex-Defined - This one is pretty different from custom fields and highlights how Flow can be enhanced and extended. This allows you to store a specific piece of data provided by a developer. It's very powerful alongside extended Flow actions like the ones provided by Gradient Works. When you select this, you'll be required to choose an "Apex Class" to define the type. This just like how you specify an Object when choosing the Record type.
  • Allow multiple values (collection) - When you specify a data type, you also can click this checkbox. If you check this, you're telling Flow to let you store a list (aka a "collection") of things with the Data Type you've chosen. Think of it as the difference between storing a single number (unchecked) or a list of many numbers (checked).
  • Availability Outside the Flow - This is an advanced topic that we'll cover in another post. You'll rarely need to check one of these at all.

Setting a variable's value

The awesome thing about a variable is its value can, well, vary. There are multiple ways to set a variable's value but we'll start here with the Assignment element.

First, let's say you've defined a variable in your flow named "Account_Name" with a data type of Text.

Create an Account_Name variable in Salesforce Flow

Now, let's use an Assignment element to assign a value. If you're using Auto-Layout in your Flow Builder, click the + button. If you're not, go to the Elements toolbox and drag an Assignment onto your canvas. This will open a new dialog called "New Assignment".

Assign the Account_Name variable value in Salesforce Flow

Every element has a Label [1] and an API Name [2]. In this case we set those to "Set Account Name" and "Set_Account_Name". Now, let's do the assignment. Start typing your variable name under Variable [3] and select "Account_Name" when it pops up. Leave your Operator [4] as "Equals". Finally, let's set the Value [5] to "Acme". It's easiest to think of this as a sentence that starts with "set": "Set the value of the Account_Name variable equal(s) to Acme"

 

Data loader alternative-moving accounts

 

Using a variable's value

Now that you've got your account name, you'd like to use it to get an account with the name you used in the variable. In this case we'll use the Get Records element. Just like with the Assignment element before you'll either click the + in the Auto-Layout or drag the element onto the canvas from the Elements toolbox.

Use the Account_Name variable to retrieve an Account in Salesforce Flow Set the Label [1] to "Get Account" and the Api Name [2] to "Get_Account". We also need to specify that we want to use the Account object [3].

Now here's the part where we actually use our variable to find an Account using the Account_Name variable. We select the name Field [4] and use it to find an Account where the name equals the value [5] of our Account_Name variable. To select the Account_Name variable, just start typing "Account_Name" in the value field and click the suggestion when it pops up.

Where to go from here

The example above was, admittedly, oversimplified. You could have saved yourself some work and just typed "Acme" into the Get Records element conditions instead of using a variable. Remember, though, variable values can vary in all sorts of interesting ways and they let you do dynamic things like:

  • Use a screen flow to let a user specify a value that you use later on to change another record
  • Set a variable to the value of field from a record that matches a query and use it to update a different field in another record
  • Use collection variables with loops to do something multiple times
  • Count the number of times you do something
  • Set something to true or false using a decision and then use it to perform some steps in some cases but not others

While variables might take some getting used to, they make all kinds of things possible with Flows that are very difficult with other forms of Salesforce automation like Process Builder. Take some time to learn them and you'll see all kinds of ways to apply them. Happy Flowing.

Related Posts