Sub topic 3
Child-to-parent: selecting parent fields with dot notation
This chapter assumes you know basic SOQL (SELECT, FROM, WHERE, ORDER BY, LIMIT). It also assumes you know what a lookup or master-detail field is, and that you can tell a field name from a relationship name. If the last point is shaky, review the previous chapter first.
The working model
A child-to-parent query starts from the child object, the one that holds the lookup or master-detail field. It reaches up to the parent by writing the relationship name, a dot, and a parent field. Salesforce follows the relationship for you. You don't write a JOIN.
For standard objects the relationship name is the field name without the Id suffix. The AccountId field on Contact has the relationship name Account. For custom relationship fields, the field Project__c has the relationship name Project__r.
Selecting parent fields
This query reads fields from the parent Account and from the Account's owner:
SELECT Id, LastName, Account.Name, Account.Owner.Name
FROM Contact
WHERE Account.Industry = 'Technology'
ORDER BY Account.NameEach result row is a Contact. The Account.Name value comes from the related Account. Account.Owner.Name goes one level further, which the later chapter on multiple parent levels covers in detail.
The same pattern works on custom objects. Here Task__c has a lookup field Project__c, so the path uses Project__r:
SELECT Id, Name, Project__r.Name
FROM Task__c
WHERE Project__r.Status__c = 'Active'Field name versus relationship name
The most common error is using the wrong one. The field holds the ID. The relationship holds the path to the parent record.
| You want | Standard example | Custom example |
|---|---|---|
| The parent's ID | `AccountId` | `Project__c` |
| A parent field's value | `Account.Name` | `Project__r.Name` |
SELECT Account FROM Contact fails, because Account is not a field you can select on its own. SELECT Account.Name FROM Contact works.
What happens when the parent is empty
A lookup field can be empty. If a Contact has no Account, the query still returns that Contact, and the parent values are null. The query does not fail and does not drop the row. This behavior matters most when you read the result in Apex.
Contact c = [SELECT Id, Account.Name FROM Contact WHERE AccountId != null LIMIT 1];
String accountName = c.Account.Name;
Contact c2 = [SELECT Id, Account.Name FROM Contact LIMIT 1];
String safeName = c2.Account?.Name;In the first query, the WHERE AccountId != null filter guarantees a parent exists, so c.Account.Name is safe. The second query has no such filter. If that Contact has no Account, c2.Account is null, and c2.Account.Name would throw a null-reference error. The safe navigation operator ?. returns null instead.
Common errors
- Selecting the relationship alone. Use
Account.Name, notAccount. - Using
__cin the path. Custom paths need__r. - Assuming the parent exists. Check for null or use
?.in Apex. - Reading a parent field you did not select. If you query
Account.Nameand then readc.Account.Industryin Apex, you get an error saying the field was not retrieved. - Mistyping the relationship name. For custom fields, the relationship name is set in the field definition, and it may differ from the field's API name. Check the field's setup page if a path is rejected.
Check your understanding
Exercise
A custom object Invoice_c has a lookup field Customer_c to Account. Write a query that returns each invoice's Id and the name of its customer, only for customers in the Technology industry. What is the relationship name you used?
Show answer
SELECT Id, Customer_r.Name FROM Invoicec WHERE Customerr.Industry = 'Technology'. The relationship name is Customerr, which is the field name with c replaced by _r (assuming the default relationship name).
Exercise
A query selects Id and Account.Name from Contact. In Apex, you read c.Account.Name for a Contact that has no Account. What happens, and how do you avoid it?
Show answer
The Account value on the Contact is null, so c.Account.Name throws a null-reference error. Avoid it by filtering with WHERE AccountId != null, by checking c.Account != null first, or by using safe navigation: c.Account?.Name.