Sub topic 2
Relationship names: __r suffixes and plural child names
This chapter assumes you can write basic SOQL (SELECT, FROM, WHERE, ORDER BY, LIMIT). It also assumes you know the difference between lookup and master-detail fields. Here you learn the naming rules that decide what you type in a relationship query.
Field names and relationship names are different things
A relationship field, such as a lookup, stores the ID of another record. A relationship name is a separate label that SOQL uses to follow that ID to the related record.
Take a Contact. Its AccountId field holds an ID. To read the account's name, you do not write AccountId.Name. You write Account.Name. AccountId is the field name. Account is the relationship name.
The two names are not interchangeable:
- Use the field name to read or filter on the ID itself, such as
WHERE AccountId = '001...'. - Use the relationship name to reach fields on the related record, such as
Account.Name.
Child-to-parent names
When you query from a child to its parent, you use the relationship name of the lookup or master-detail field.
| Field type | Field API name | Relationship name |
|---|---|---|
| Standard lookup | `AccountId` | `Account` |
| Standard lookup | `OwnerId` | `Owner` |
| Custom lookup | `Project__c` | `Project__r` |
For a standard field, drop the Id suffix. For a custom field, replace __c with __r. Custom relationship names always end in __r when you use them in a query.
SELECT Id, Name, Project__r.Name
FROM Task__c
WHERE Project__r.Status__c = 'Active'Here Project__c is the field on Task__c. Project__r is the name that lets the query reach Name and Status__c on the parent Project.
Parent-to-child names
When you query from a parent to its children, you use the child relationship name. It is plural, and it describes the collection of child records.
For standard objects, Salesforce sets these names. Account has Contacts, Opportunities, and Cases. You use the name as the target of a subquery in the SELECT list.
SELECT Id, Name,
(SELECT Id, LastName FROM Contacts ORDER BY LastName LIMIT 5)
FROM Account
WHERE Industry = 'Technology'The subquery says FROM Contacts, not FROM Contact. It names the relationship, not the object.
For custom relationships, the child relationship name comes from the setting on the lookup or master-detail field. When you create the field, you enter a Child Relationship Name, such as Tasks. In a query, Salesforce adds __r.
SELECT Id, Name,
(SELECT Id, Name FROM Tasks__r)
FROM Project__cFinding the right name
Do not guess. Check the real names in one of these places:
- In Setup, open Object Manager, choose the child object, and open the lookup or master-detail field. The detail page shows the field name and the Child Relationship Name.
- In the Salesforce object reference, find the child object. Parent objects list their child relationships there.
- In Apex, call
Schema.SObjectType.Account.getChildRelationships()and read eachgetRelationshipName().
Common naming errors
- Using the field name in a path.
Project__c.Namefails. WriteProject__r.Name. - Using the singular object name in a subquery.
FROM Contactinside a parent query fails. WriteFROM Contacts. - Forgetting the suffix on a custom name.
FROM Tasksfails when the relationship isTasks__r. - Mixing up direction. A path with a dot (
Account.Name) goes up to a parent. A subquery in parentheses goes down to children.
Exercise
A custom object Invoice__c has a lookup field Customer__c to Account. You define its Child Relationship Name as Invoices. Write the field to select the account's name from an invoice query, and write the subquery source name to select invoices from an account query.
Show answer
From an invoice, select Customer__r.Name. From an account, use (SELECT Id FROM Invoices__r). The field name Customer__c becomes Customer__r for the parent path, and the child relationship name Invoices becomes Invoices__r in the subquery.