Sub topic 10
Referencing related records and global variables
This chapter assumes you can already write an error condition formula with AND, OR, and ISBLANK, and that you know lookup and master-detail relationships. Here you extend a rule beyond the record being saved. You will read fields from parent records and from the current user.
Reading parent fields with dot notation
A validation rule can read fields on any record that the current record points to through a lookup or master-detail field. Write the relationship name, a dot, then the field name.
On an Opportunity, Account.BillingCountry is the billing country of its Account. Owner.Email is the email of the Opportunity owner. For custom relationship fields, the relationship name ends in __r, for example Region__r.Manager__c.
You can chain relationships to move further up. Account.Owner.Manager.Id reaches the manager of the Account owner. A formula can span at most 10 unique relationships, and each hop counts toward that limit.
AND(
ISPICKVAL(StageName, "Closed Won"),
ISBLANK(Account.BillingCountry)
)This rule blocks an Opportunity from reaching Closed Won when its Account has no billing country. The error message might read: "Add a billing country to the Account before closing this deal."
Limits of cross-object references
- A rule reads only upward, from child to parent. It cannot read a list of child records.
- To validate against child data, such as the number of Contacts, first create a roll-up summary field on the master record. A roll-up summary works only on master-detail relationships.
- A rule runs when the record it belongs to is saved. If someone edits the Account later, existing Opportunities are not rechecked until they are saved again.
- If the lookup is empty, every parent field reads as blank. Guard against this with ISBLANK where it matters.
Global variables
Global variables describe the context of the save instead of the record's own data. These are the ones you will use most:
| Variable | What it gives you |
|---|---|
| `$User` | Fields of the current user, such as `$User.Id` |
| `$Profile` | The current user's profile, such as `$Profile.Name` |
| `$UserRole` | The current user's role, such as `$UserRole.Name` |
| `$Permission` | True or false for a custom permission, such as `$Permission.Can_Edit_Opportunity_Amount` |
| `$RecordType` | Fields of the record's record type, such as `$RecordType.Name` |
The next rule lets only the record owner or a system administrator change the Amount:
AND(
ISCHANGED(Amount),
OwnerId <> $User.Id,
$Profile.Name <> "System Administrator"
)All three conditions must be true to block the save. A change by someone who is neither the owner nor an administrator triggers the error.
<br>
Common mistakes
- Referencing a parent field without checking for an empty lookup. A blank parent can make the rule pass or fail in ways you did not intend.
- Expecting a rule to fire when only the parent changes.
- Mixing up
$User(the person saving) withOwner(the person who owns the record). They are often different people.
Exercise
Write an Opportunity validation rule formula that blocks the save when the Account's Industry is blank and the user is not a system administrator.
Show answer
AND(ISBLANK(Account.Industry), $Profile.Name <> "System Administrator"). The formula returns true, and so blocks the save, only when both conditions hold.