Make the Most of Global Variables and Values
Learning Objectives
After completing this unit, you’ll be able to:
- Reference the running user’s fields in Flow Builder.
- Reference the running user’s role and profile information in Flow Builder.
- Reference custom labels in Flow Builder.
- Reference custom metadata in Flow Builder.
What Is a Global Variable?
Global variables exist in every flow. You don’t create them and you can’t change their values in the flow, but they give your flow access to very useful data that can be different each time the flow runs.
You can use global variables in fields that let you select a resource. Such fields usually contain placeholder text such as “Enter value or search resources…” or “Insert a resource…”
In Flow Builder, global variables behave similarly to objects and fields: each global variable contains multiple values. To use a global variable in a flow, follow these steps.
- While creating or editing an element, click in a field that lets you select a resource. Flow Builder displays the resources you can place in this field.
- Under Global Variables, select a variable that starts with $. Flow Builder displays the fields available to the selected variable.
- Select the field that holds the value that you want to use.
Get the Running User’s Field Values
The $User global variable (or, in some elements, the Running User global variable) gives your flow a glimpse at the user behind the keyboard. We don’t mean that literally, of course. But it does give your flow access to the user data of the user that runs your flow. Use the $User global variable to access data like the running user’s ID, name, manager ID, email address, and much more.
Pyroclastic’s sales managers want to know when a team member closes a high-value opportunity as lost. Flo asks you to automate that notification with a Chatter post to the opportunity owner's manager. The person closing the opportunity might not be the opportunity’s owner, so the notification should indicate who made the change.
You already have a flow that runs when the StageName of an opportunity changes to Closed Lost: Create Follow-Up with Decision Maker. Add an action in that flow to make a Chatter post to the opportunity owner’s manager.
First, you need to identify the owner’s manager. As you did in unit 2, use a Get Records element to retrieve information and then use that information in an Action element.
Retrieve the Owner’s Manager
- Open the Create Follow-Up with Decision Maker flow.
- On the flow canvas, after the Get Decision Maker element, click . Select Get Records.
- For Label, enter
Get Owner Manager
.
Remember, this name is used to label the generated variable, so it’s a good idea to use a descriptive name.
- For Object, select User.
- For Condition Requirements, select All Conditions Are Met (AND).
- In the Filter Records section, define conditions that tell the element which records to retrieve:
- Field: Id
- Operator: Equals
- Value: $Record > Owner > ManagerId
- Click in the New Get Records panel to close the panel.
You now have a record variable that contains all the fields from the opportunity owner’s manager, including their name and email address. Next, you use an Action element to create the Chatter post.
Create a Text Template
- In Flow Builder, if the Toolbox isn’t already open, click to open it.
- Click New Resource.
- For Resource Type, select Text Template.
- For API Name, enter
ChatterPostManager
.
- Next to the Resource Picker field, change View as Rich Text to View as Plain Text.
- In the Resource Picker field, select User from Get_Owner_Manager > Id.
A merge field for the manager’s name is inserted at the beginning of the message body.
- Add square brackets [ ] around the merge field, like this:
[{Get_Owner_Manager.Id}]
- Add an @ symbol at the beginning and a colon at the end, like this:
@[{Get_Owner_Manager.Id}]:
- After the colon, start a new line and enter:
This opportunity was marked Closed Lost. Closed by:
- Leave a space and then insert three more merge fields using the $User global variable, found in the Global Variables section. Make sure to add a space between them so they don’t run together:
-
$User > FirstName
-
$User > LastName
-
$User > Email
[Alt text: The New Resource window corresponding to the previous steps.]
- Click Done.
Next, you use the text template in an Action element to post to the opportunity’s Chatter feed.
Notify the Manager in a Chatter Post
- After the Get Owner Manager element, click . Enter
post
in the Search bar.
- Select Post to Chatter.
- For Label, enter
Chatter Post to Manager
.
- For Message, select the ChatterPostManager text template.
- For Target Name or ID, select Triggering Opportunity > Owner ID.
- Click Done.
- Save the flow.
Get the Running User’s Profile and Role Values
When creating flows, avoid hardcoding IDs. Hardcoding is when you manually enter a Salesforce ID into flows or code. An example of hardcoding in an Update Records element is setting criteria to check for “005i000006rZRFU”. Instead, you should retrieve the ID with a Get Records element, and check the ID that’s stored in the Get Records element’s record variable.
What does this have to do with global variables? At some point, you might want to access the user’s profile or role. Suppose your organization is divided into territories, assigned as user roles to salespeople. The Western territories (Sales-West1, Sales-West2, and Sales-West3) use a different discount calculation than other territories. To calculate discounts correctly, a flow needs to get the running user’s role to confirm whether they’re assigned to one of the Western territories.
Flo tries to use the $User global variable to find the running user’s role, but the only role value there is the role’s ID. She knows better than to hardcode an ID in her flow. Fortunately, there’s another way to access information from the running user’s role: the $UserRole global variable (or, in some elements, the Running User Role global variable). It can access many values in the running user’s role.
Likewise, the $Profile global variable (or, in some elements, the Running User Profile global variable) should be used in similar situations where the flow needs to access the running user's profile values. The only profile value in $User is the profile's ID, but just like $UserRole for roles, $Profile grants access to the rest of the profile's values.
To avoid hardcoding when checking the running user’s role, reference $UserRole > DeveloperName. For profile, reference $Profile > Name.
Retrieve Custom Labels
If your organization’s users work in multiple languages, custom labels are very valuable. Custom labels contain a string of text that you can translate into multiple languages. After you define the translations, Salesforce displays custom labels to each user in the user’s native language. You can even use custom labels in flows.
In Flow Builder, use the $Label global variable (or, in some elements, the Custom Label global variable) to place a custom label anywhere that accepts a text resource, such as screen components, default values for fields, and text formulas. In this example, creating a task record, the value of the Subject field is set to the TaskPrintLabels global variable. Now when users view the task, its subject is in their own language.
The next example is a Display Text component on a flow screen. In the Resource Picker field, when $Label is selected, available custom labels are listed. Selecting one adds it to the text area in the component.
Other Important Global Variables
There are a few other global variables that can be useful when creating flows.
Global Variable |
What It Does |
---|---|
$Flow.CurrentDate (or Running Flow Interview > CurrentDate)
|
Retrieves the date that the element is run |
$Flow.CurrentDateTime (or Running Flow Interview > CurrentDateTime)
|
Retrieves the date/time that the element is run |
$Flow.InterviewStartTime (or Running Flow Interview > InterviewStartTime)
|
Retrieves the date/time that the flow first started running |
$Flow.FaultMessage (or Running Flow Interview > FaultMessage)
|
Retrieves the error message when a flow stops due to an error |
$Organization (or Running Org)
|
Retrieves information found on the Company Information page in Setup, such as your org’s name or address |
$Record (or Triggering [object name])
|
In a record-triggered flow, $Record retrieves data from the record that triggered the flow. (You used this global variable in a hands-on challenge to get the Company and ID of a newly created lead.) You can also use $Record to:
Learn more about $Record in the Record-Triggered Flows badge. |
Retrieve Custom Metadata Values
Custom metadata isn’t a global variable, but it is a set of values that you can access globally: in Flow Builder and throughout your Salesforce org. For example, a custom metadata type can be used across flows, code, validation rules, and more.
In Flow Builder, you retrieve custom metadata in the same way you retrieve object records: using the Get Records element. When selecting the object, select the custom metadata type.
For more information and sample use cases, check out the Custom Metadata Types Basics Trailhead module, particularly the Using Custom Metadata Types in Flow unit.
Resources
- Salesforce Admins Blog: Why You Should Avoid Hard Coding and Three Alternative Solutions
- Salesforce Admins Automate This! Video: Automate This! — Send Case Notifications Automagically with Flow
- Trailhead: Custom Metadata Types Basics