Sunday, April 14, 2013

Salesforce Global Variable

In previous blog, we talk about Custom Label, where System Administrator able to set values in Custom Label, it is accessible via formula field using $Label. $Label is one of Global Variable in Salesforce, you can use global variable to reference general information about the current user or your organization. Global variable can be easily identified with prefix $.

Components such as custom buttons, custom links, formulas, and Visualforce pages allow you to use global variables with special merge fields to reference the data in your organization. Please note, global variables accessible in formula field, validation rule and custom link / button are different. See screenshot below:

in formula field

in validation rule

in custom button / link

Here a few often used Global Variable merge field for validation rule or formula field:

$Profile -- A global merge field type to use when referencing information about the current user’s profile. Use profile merge fields to reference information about the user’s profile such as license type or name.

$User -- A global merge field type to use when referencing information about the current user. User merge fields can reference information about the user such as alias, title, and ID.

$RecordType -- A global merge field to use when referencing the record type of the current record.
Although this merge field is not visible in screenshot above, but it will work. You can use following merge fields: $RecordType.Id, $RecordType.Name

$Organization -- A global merge field type to use when referencing information about your company profile. Use organization merge fields to reference your organization’s city, fax, ID, or other details. If you use this merge field with $Organization.Id and reference to your production instance, when you create/refresh sandbox, Id value in the validation rule will change to new sandbox Org Id.

Some use cases using Global Variable:

1. To ensure that only the record owner or users with profile System Administrator can make changes to Opportunity Amount.
Create validation rule in Opportunity with following Error Condition Formula:
AND( ISCHANGED( Amount ), Owner <> $User.Id, $Profile.Name <> "System Administrator" ) 

2. Not allow users with profile System Administrator or users with first name = Steve to create or modify Account
Create validation rule in Account with following Error Condition Formula:
OR($User.FirstName = 'Steve', $Profile.Name = 'System Administrator')

3. To view all Account last modified by me (assume Account sharing Organization-Wide Defaults is Public)
Create a checkbox formula field "Last Modified by Me" in Account with formula:
$User.Id = LastModifiedById

"Last Modified by Me" value will be dynamic based on current login user. Then, create a List View with filter "Last Modified by Me" = True (the same can be applied for Report).

You can share this view to all users or certain groups of users without each user need to create their own view for the same purpose.

This kind of simple formula can be used for Created By Me or any user lookup custom field in any object.