Sub topic 5
Walking up several parent levels
This chapter assumes you can write basic SOQL (SELECT, FROM, WHERE, ORDER BY, LIMIT). It also assumes you know that a child-to-parent query reads parent fields with dot notation, and that a lookup field has a relationship name such as Project__r or Account.
What multi-level traversal is
A child-to-parent query can follow more than one relationship in a single path. Each dot moves one level up. Starting from a Contact, Account.Name goes up one level. Account.Owner.Name goes up two levels: from the Contact to its Account, then from the Account to the User who owns it.
You can use these paths anywhere a field is allowed: in the SELECT list, in WHERE, and in ORDER BY.
SELECT Id, LastName, Account.Name, Account.Owner.Name
FROM Contact
WHERE Account.Industry = 'Technology'
ORDER BY Account.NameIn this query, Account.Owner.Name is the name of the user who owns the contact's account. It is not the owner of the contact.
Reading the path
Each segment of the path, except the last one, must be a relationship name. The last segment is a field on the final object. Take Account.Owner.Name:
Accountis the relationship name of theAccountIdlookup on Contact.Owneris the relationship name of theOwnerIdfield on Account.Nameis a field on User.
For a custom lookup, replace the field name's __c with __r. A custom path might read Task__r.Project__r.Name, assuming Task__c has a lookup to Project__c. Standard relationships have no suffix. AccountId becomes Account, and OwnerId becomes Owner.
How many levels you can go
SOQL allows up to five levels of parent-to-child traversal in a single query, written as dots in a path. The limit applies to the number of relationships between the starting object and the field you read. A path that goes through five relationships is allowed. A sixth one fails when you run the query.
This is a small, fixed limit. If you hit it, split the work into two queries. Query the intermediate object first, then use its IDs in a second query.
Null parents
A multi-level path can break at any level. If a Contact has no Account, then Account.Owner.Name returns null. The query does not fail. The same applies if the Account exists but has no value at the next level.
In Apex, the null shows up when you read the result. Reading c.Account.Owner.Name throws a null-reference error if c.Account is null. Safe navigation (?.) covers each level.
Contact c = [
SELECT Id, Account.Name, Account.Owner.Name
FROM Contact
LIMIT 1
];
String ownerName = c.Account?.Owner?.Name;
System.debug('Account owner: ' + ownerName);Because ?. stops at the first null and returns null for the whole expression, one line handles every missing level.
Filtering and sorting on deep paths
A deep path works in WHERE and ORDER BY the same way it does in SELECT. This query returns contacts whose account owner has a given title, sorted by that owner's name:
SELECT Id, LastName, Account.Owner.Name
FROM Contact
WHERE Account.Owner.Title = 'Sales Manager'
ORDER BY Account.Owner.NameA WHERE condition on a parent path excludes child records whose parent is null, because null does not equal 'Sales Manager'. If you need those records, add a separate condition for the lookup field, such as OR AccountId = null.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Using the field name `AccountId` in a path, as in `AccountId.Name` | Query error | Use the relationship name: `Account.Name` |
| Using `Owner__r` for the standard owner field | Query error | Use `Owner` |
| Forgetting `__r` on a custom lookup in the middle of a path | Query error | Use `Project__r.Owner.Name`, not `Project__c.Owner.Name` |
| Reading a deep path in Apex without null checks | Null-reference error | Use `?.` at each level |
Exercise
Write a query on Task_c that returns the Id and the name of the owner of the related Projectc. Assume Taskc has a lookup field Project_c. Then write the Apex expression that reads that owner name from a record t without a null-reference error.
Show answer
SELECT Id, Project_r.Owner.Name FROM Taskc. In Apex: String ownerName = t.Project_r?.Owner?.Name;