Sub topic 11
Relationship Fields and What They Connect
This chapter assumes you know what objects, records and fields are, and that you can find an object's fields in Object Manager. It covers the relationship field types and what each one links. The next chapter covers how to choose between lookup and master-detail.
What a relationship field does
A relationship field stores the ID of a record on another object. The object that holds the field is the child. The object it points to is the parent. The field is the foreign key, and Salesforce shows it on the page as a clickable link to the parent record.
In SOQL, the child has a relationship name that lets you reach the parent's fields. For a custom field Region__c, the relationship name is Region__r.
The relationship field types
| Type | Parent object | Notes |
|---|---|---|
| Lookup | Another Salesforce object | The link is optional by default. The child keeps its own owner and sharing. |
| Master-detail | Another Salesforce object | The link is required. The child inherits the parent's sharing and is deleted with it. |
| Hierarchical | The same object | Available only on User. It links a user to a manager, for example. |
| External lookup | An external object | The parent lives in an external system. The match uses the parent's external ID. |
| Indirect lookup | An external object as the parent is the child's side | The child is an external object. It matches a Salesforce field marked as an external ID and unique. |
Lookup and master-detail are the two you will use most often. A lookup to the same object, such as a parent account on Account, is a self-relationship. It is separate from the hierarchical type, which exists only on User.
Seeing the link in code
A relationship name works in both directions. From the child you go up with the __r name. From the parent you go down with the plural child relationship name, in a subquery.
SELECT Id, Name, Account.Name FROM Contact LIMIT 5SELECT Id, Name, (SELECT LastName FROM Contacts) FROM Account LIMIT 5The first query reads a parent field through the standard Account relationship. The second returns each account with its child contacts.
Things to check when you create one
- The field label and API name can differ from the related list name. The related list name appears on the parent's page layout.
- A lookup can be set to clear the child's field when the parent is deleted, or to block the deletion. The default is to clear it.
- A master-detail field cannot be added to an object that already has records unless you first create it as a lookup and fill every row, then convert it.
- An object has a limited number of master-detail relationships. Check the current limit in the Salesforce documentation before you plan a design with several.
Exercise
A custom object Invoice_c has a lookup field Customer_c to Account. Write the SOQL that returns each invoice's Id and its customer's Name.
Show answer
SELECT Id, Customer_r.Name FROM Invoicec. The custom field ends in c, so the relationship name ends in _r.