Sub topic 6
Reading fields from related records
This chapter assumes you know how to create a formula field, choose its return type, and tell lookup relationships from master-detail relationships. Here you will learn how a formula reads fields from other records through relationships. This is called cross-object formula traversal.
How relationship traversal works
A formula can read a field on a related record by following the relationship with dot notation. The path starts at the object that holds the formula. Each step names a relationship, and the last part names the field to read.
On Opportunity, Account.Name follows the Account relationship and reads the account's name. You can chain steps to go further. Account.Owner.Name goes from the opportunity to its account, then to the account's owner, and returns the owner's name.
Account.Owner.NameCreate this as a Text formula field on Opportunity. The return type must match the value the path ends on.
Relationship names for custom fields
Standard lookups use the field name without its suffix, such as Account. Custom lookups and master-detail fields end in __c, and the relationship name ends in __r. A custom lookup field Region__c on Account is read through Region__r.
| Field type | Lookup field | Relationship name in a formula |
|---|---|---|
| Standard lookup | `AccountId` | `Account` |
| Custom lookup | `Region__c` | `Region__r` |
For example, Account.Region__r.Manager__c reads a custom field on the related Region record.
Which direction works
Formulas can only look up, from a child record to its parent. They cannot reach down from a parent to its children. To summarize child records on a parent, you need a roll-up summary field. Later chapters cover that.
Behavior to expect
- A formula returns a blank value when any record on the path is missing. An opportunity with no account gives a blank
Account.Owner.Name. - The formula recalculates when it is read. It always shows the current value of the related field.
- You can only read fields you have access to. Field-level security on the related field affects what users see.
- Each new relationship step adds to the formula's limit on distinct object references. The next chapters cover that limit.
Combining traversal with other functions
Traversal works anywhere a field works. You can place it inside IF, CASE, or BLANKVALUE. A path that crosses relationships can be blank, so wrap it when you need a default.
BLANKVALUE(Account.Owner.Name, "Unassigned")Exercise
An Opportunity formula must show the name of the manager of the account's owner. The Manager field is a standard lookup on User. What path should the formula use, and what return type should it have?
Show answer
Use Account.Owner.Manager.Name, with a Text return type. The path starts at Opportunity, goes to Account, then to Owner (a User), then to that user's Manager, and ends on the Name field.