Sub topic 6
Reading fields from related records
Cross-object formulas let a formula on one object display or use values from records it points to. This chapter covers how relationship traversal works, the syntax for each direction, and the errors you will meet. It assumes you can create a formula field in Setup and know the difference between lookup and master-detail relationships.
What a cross-object formula does
A cross-object formula is a formula that references a field on a related record. The formula follows a relationship field (a lookup or master-detail field) to the parent record and reads a field there. Salesforce calls this following a relationship, or traversing it. The dot character separates each step.
Formulas can only look "up" from a child to its parent. A formula on Opportunity can read from Account. A formula on Account cannot read individual Opportunity fields. To bring child data upward, you use a roll-up summary field, which later chapters cover.
The value is calculated each time the record is read, so it always reflects the current parent data. You do not need a trigger or a Flow to keep it current.
Relationship names and field names
Traversal uses the relationship name, not the field name. This distinction causes most beginner errors.
- Standard relationship fields, such as
AccountIdon Opportunity, use the name withoutId:Account. - Custom relationship fields end in
__c, such asProject__c. Their relationship name ends in__r:Project__r.
| Field type | Field API name | Name used in a formula traversal |
|---|---|---|
| Standard lookup | `AccountId` | `Account` |
| Standard lookup | `OwnerId` | `Owner` |
| Custom lookup | `Project__c` | `Project__r` |
| Custom master-detail | `Invoice__c` | `Invoice__r` |
The Insert Field button in the formula editor builds these paths for you. Use it when you are unsure of a name. It lists related objects and lets you drill into them one level at a time.
A first traversal
The simplest cross-object formula is just a path. This Text formula on Opportunity shows the name of the account's owner:
Account.Owner.NameIt takes two steps. Account moves from the opportunity to its account. Owner moves from the account to the user who owns it. Name reads that user's name. Each hop follows one relationship. The formula returns Text because Name is a text field.
A hop can also read a field directly without going further:
Account.IndustryIf the opportunity has no account, the formula returns blank. The next section explains why that matters.
Blank parents
A traversal through an empty relationship does not raise an error. A formula that reads a field through an empty lookup returns blank for Text, or null for Number, Currency, Percent, and Date. This matters when you use the result in a calculation.
Take a Number formula that doubles the account's employee count:
Account.NumberOfEmployees * 2If the opportunity has no account, or the account has no employee count, a null value feeds into arithmetic. Salesforce treats the null as zero in many cases. Do not rely on that, because behavior differs by field and formula setting. Use BLANKVALUE to be explicit:
BLANKVALUE(Account.NumberOfEmployees, 0) * 2You can also test the relationship itself with ISBLANK:
IF(ISBLANK(AccountId), "No account", Account.Name)The test uses AccountId, the field on the record, rather than the traversal path. This is a reliable way to check whether a relationship is populated.
Combining traversal with other functions
A traversed field behaves like any other field in a formula. You can use it in IF, CASE, TEXT, and comparisons. This Text formula on Opportunity flags deals whose account is in a strategic industry:
IF(ISPICKVAL(Account.Type, "Customer - Direct"),
"Existing customer",
"New business")ISPICKVAL is required because Account.Type is a picklist. Picklist fields cannot be compared with = or used directly in text functions. Use ISPICKVAL for tests and TEXT to convert to a string:
"Industry: " & TEXT(Account.Industry)Walking up several levels
You can chain relationships to reach grandparents and beyond. A custom example: a Task__c object has a lookup to Project__c, which has a lookup to Program__c. A formula on Task__c can read the program's name with:
Project__r.Program__r.NameThere is a hard limit: a formula can span at most 10 unique relationships. A path like Account.Owner.Name crosses two relationships. Every distinct relationship you reference anywhere in the formula counts, and repeating the same relationship does not count twice. Chapter on formula limits covers this in more detail.
Polymorphic relationships
Some standard fields, such as Owner on certain objects or Who and What on Task, can point to more than one object type. These are polymorphic relationships. A formula can read only the fields common to all target types, for example Owner.Name. Reaching a type-specific field needs special handling, and in many cases is not possible in a formula.
What cross-object formulas cannot do
Keep these restrictions in mind when you design a field:
- A formula can only go up to parents. It cannot read from child records.
- A formula cannot reference a field on a related record if that field is itself a formula that would create a circular reference.
- Not every field type is available in every traversal. Long text area fields cannot be used in formulas.
- The formula must still fit the compile size limit. Long traversal paths expand when compiled, so a formula that looks short can exceed the limit.
- Users see the formula result even if they lack access to the parent field. Field-level security on the parent field does not hide the value in the formula. Consider this before exposing sensitive data.
Cross-object changes and recalculation
Because the formula is calculated when the record is read, there is nothing to refresh. If an account's industry changes, every opportunity formula showing Account.Industry displays the new value the next time it is viewed or queried.
The cost appears elsewhere. Reports and list views that filter or sort by a cross-object formula must evaluate the formula for each row, which can slow large queries. Formula fields also cannot be indexed by default, so filtering on them in SOQL can cause full scans on large objects. If you need to filter often on a parent value, consider copying the value into a real field with a Flow, or ask Salesforce Support about a custom index on the formula.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Error: Field Project__c is a relationship, not a field` or a similar "field does not exist" message | The custom field name was used instead of the relationship name | Use `Project__r` when traversing |
| `Field Account.Type is a picklist field. Picklist fields are only supported in certain functions.` | The picklist was used with `=` or `&` | Wrap with `ISPICKVAL` or `TEXT` |
| `Formula has too many unique relationships` | More than 10 distinct relationships are referenced | Simplify, or store an intermediate value in a field |
| `Compiled formula is too big to execute` | Deep traversals expand when compiled | Break the logic into helper formula fields |
| Blank result where you expected a value | The parent lookup is empty on that record | Guard with `ISBLANK` or `BLANKVALUE` |
Design guidance
- Prefer a formula when you only need to display a parent value or use it in a simple calculation on the child.
- Use short, well-named helper formula fields when a calculation needs many hops. They make each formula easier to read and keep the compile size small.
- Check for empty relationships whenever the parent is optional.
- Avoid exposing parent fields that the viewing user should not see.
Exercise
An Opportunity formula must show the name of the account's owner, but opportunities without an account should display "No account". Write the formula and say what it returns when the account exists but has no owner name entered.
Show answer
Use IF(ISBLANK(AccountId), "No account", Account.Owner.Name). The test checks AccountId on the opportunity itself. When the account exists, the formula returns the owner's name. An account always has an owner, so the name is normally filled in. If it were somehow blank, the formula would return a blank Text value, not an error.
Exercise
You have a custom lookup field Project__c on Task__c. A formula on Task__c uses Project__c.Name and fails to save. What is wrong and what is the fix?
Show answer
Traversal requires the relationship name, which for a custom field ends in __r, not __c. Change the formula to Project__r.Name.