Salesforce provides a variety of global variables for use in formula fields, Visual Flow, Visualforce pages, and other places. But what are global variables anyway?
What are Global Variables?
Global variables are “merge fields” that are used to access information about the current state of the platform, such as information about the current Organization, current User, and current Record. These variables always begin with the dollar character (“$”).
Examples of Global Variables
What is a “Merge” Field?
A merge field is a field that must be paired with another merge field in order to return a value. (The clue here is in the word “merge,” a “merge” field must be “merged” with another “merge” field in order to yield a value!)
Examples of Global Variable – Merge Field Pairs
- $Organization.Name, $Organization.Country
- $RecordType.Name, $RecordType.Id
- $User.Firstname, $User.Lastname, $User.Id
What Do Global Variables Do?
Here is how I think about global variables. I imagine that Salesforce has an object that has a bunch of fields and each field is a pointer to the current object. Now I have no idea if this is really the case, it just helps me make sense of it in my head.
We can access values in fields on current objects by using the global (“$”) field name combined with a field name on the current object in the dot-notation syntax.
For instance, to get the user’s first name we would use $User.Firstname, exactly like referencing a field on any object (Account.Name).
Where Can Global Variables Be Used?
Global variables can be used in a variety of places:
- Custom formula fields
- Visual Flow and Process Builder
- Validation rules
- Visualforce pages
- Apex classes and triggers
The list of variables, however, varies from place to place as well as whether or not certain resources are being utilized. For instance, the list of global variables available in a Formula field will be different than the list in Visual Flow. And certain global variables, like $Label and $Permission will not be available until those resources are used.
Here is are examples of global variables available in a formula field:
You can see that the list on the left does not contain $Label or $Permission. That is because in the org on the left the Label and Permissions resources have not been used.
Using Global Variables
So that’s my quick explanation of global variables. I’ll post a bit more detail on some of these variables in a bit.
- Salesforce Help Documentation – Understanding Global variables
- Visualforce Developer’s Guide – Global Variables
- Salesforce Help Documentation – Global Variables in Visual Flow
- Referencing Global Variables in Visual Flow
- Salesforce Standard Objects
- SFDC Blog Post on Global Variables