Flow: How To Use Global Variables in Flow Builder ($Record)
Simply put, global variables capture various information at the moment the flow fires. Just like the recordId or $Record which captures the current record, you have other variables that capture who is the current user, what is the company, or when is the flow executed. You can easily identify a global variable in Flow Builder by the ’$” sign found at the start of its name (eg, $Record and $User)
Global variables are very powerful for building flow. Fortunately for us, they’re also easy to use, so let’s dive into what they are and how to use them in flows!
Explanation Of Each Global Variable
You can find the official explanations here, but again we want some really easy words and examples right? I will list them down in the order of most frequently used. (by me)
- $Record
- $Record__Prior
- $User / $UserRole / $Profile
- $Organization
- $Flow
- $Label
- $Permission
- $System
- $Setup
- $Api
$Record
What’s not to love about the $Record variable? It works just like recordId, and you don’ even need to configure anything! This variable only exists in autolaunch flow types (Anything except Screen Flow). You can use it to replicate IsChanged formula or update the record, but here are some important points to consider when using $Record:
1. If you want to update $Record in a before-save flow, you can simply use Assignment (or use an Update Records and choose “The record triggered this flow”); But when using an after-save flow, you’ll need to use both Assignment and Update Records.
2. If you use $Record in a Create or Update Records, make sure you have “Filter inaccessible fields from flow requests” checked in the automation setting.
This is because $Record also includes system and read-only fields that your users cannot modify. This setting filters them out. Otherwise, the system might try to change those fields which would make the flow fail.
Use Case Example
Important Setting To Enable
$Record__Prior
It’s an amazing new feature after Spring’21 release. Here is the summary:
- Only available in record-triggered flow type
- Only available when choosing “record is updated” or “record is created or updated”
- If the record is newly created, the values of this variable will be null
- If the field is not updated when the flow fires, that field in $Record and $Record__Prior will have the same value (only changed fields at the time the flow was triggered will have values in $Record_Prior that are different from $Record)
Use Case Example: IsChanged
* It does not matter which option you choose for “When to Execute Outcome” because of note #4 above.
$User / $UserRole / $Profile
Like the names imply, You can access the information, role, or profile of the person who’s currently running the flow through these global variables.
Use Case Example
- Show the current user’s name on a screen element
- Check the user profile or role and set up different flow paths for each scenario
$Organizaion
As you may have guessed, $Organization gives you access to your own company’s (organization) information. This is the information you can find in Company Settings in Setup.
$Flow
Basically, $Flow allows you to reference a lot of the current information about the flow interview. To see what I mean, see the case examples below. Also, find more details in this Salesforce help document.
Use Case Example
- Show current date or date/time
- Show the current status or stage of the flow
- Send out a fault message to relevant teams when the flow fails
$Label
You can check out what a custom label is in this article. My understanding is that labels work like snippets. You can create a label that contains a specific value, and reference that label to show the message.
Use Case Example: In Flow Editor
Use Case Example: When Running Flow
$Permission
$Permission will return True/False depending on whether the user has access to a specific custom permission. Learn more about custom permissions in this Salesforce help document.
$System
The only thing that is available is $System.OriginDateTime, which is a static value of 1900-01-01 00:00:00.
Use Case Example
No clue 🙂 It can be used to calculate time difference, but there are many other ways to calculate time difference too….
$Setup
You can use $Setup to get the Custom Setting from your org. However, if you want to get Custom Metadata Type, you will need to use a Get Records element. Check out this post on How To Use Custom Metadata Type.
$Api
Absolutely no clue what the use case is and how can this help. It looks like a very developer’y variable. Check out this article if you want to learn more about $Api.
Use Case Example (Shared by Eric Smith)
One of the uses of the $API global variable is to get the correct URL for the current domain. I’ve used it in a few different components. Let’s Get Toasty! and How to Include Chatter Posts on Printable Views.
LEFT({!$Api.Partner_Server_URL_340},FIND("/services", {!$Api.Partner_Server_URL_340})) & {!vContactId}
(Updated Apr. 17)
One of the uses of the $API global variable is to get the correct url for the current domain. I’ve used it in a few different components. Let’s Get Toasty! and How to Include Chatter Posts on Printable Views.
This is perfect Eric. I updated the article with your comment and quoted you. Thanks!
Hi Melody!! Love the blog!! …. I was curious… are you able to get the running user’s associated account id’s using a global variable?
Use case, website customers logging in to upload a photo in a flow.
Thanks!!
Hi Alex, the account Id is not accessible through the $User global variable but only through Get Records, $Record, or the recordId created manually, so you can simply create that and assign the recordId as the running user.
Just a note that if you are looking to change the user profile photo, I think last time I checked it was not possible, but I will keep searching for this feature (If you simply want them to upload photo, ignore this paragraph;))