Sub topic 1
Relationships connect objects: parents, children, and relationship fields
Prerequisites
This chapter assumes you know the following:
- The basic Salesforce data model: standard and custom objects, fields, and records.
- How to open Setup and the Object Manager.
- Basic SOQL
SELECTsyntax, for the short query examples.
What a relationship is
A relationship links one record to another record. In Salesforce, you create the link with a relationship field on one object. The field stores the Id of a record on the other object.
Take a Contact that belongs to an Account. The Contact has a field called AccountId. It holds the Id of one Account record. That single field is the relationship.
Parent and child terminology
Every relationship has two sides:
- The parent (also called the master) is the object that is referenced.
- The child (also called the detail) is the object that holds the relationship field.
The field always lives on the child. A parent does not store a list of its children. Salesforce finds them by looking for child records whose field points to the parent.
| Term | Where it lives | Example |
|---|---|---|
| Parent | The record being pointed to | Account |
| Child | The record that holds the field | Contact |
| Relationship field | A field on the child object | `AccountId` on Contact |
One parent can have many children. Each child record points to one parent for each relationship field it has.
Two names for each relationship
Each relationship field gives you names you use in SOQL and Apex. The name you use depends on the direction you are going.
- Going from child to parent, you use the relationship name of the field. For a custom field named
Course__c, the relationship name isCourse__r. For the standardAccountIdfield, it isAccount. - Going from parent to child, you use the child relationship name. For Contact under Account, it is
Contacts.
The two queries below show both directions. The first goes from child to parent with dot notation.
SELECT Id, LastName, Account.Name
FROM Contact
WHERE Account.Industry = 'Technology'The second goes from parent to child. It uses a subquery on the child relationship name.
SELECT Name, (SELECT LastName FROM Contacts)
FROM AccountBoth queries use the same relationship field, AccountId. Only the direction differs.
The two main relationship types
Salesforce offers several relationship field types. This tutorial covers the two you will use most:
- Lookup. A loose link. The parent is optional, and the two records mostly live independently.
- Master-detail. A tight link. The parent is required, and the child depends on it.
Later chapters cover each type in detail. They also show how two master-detail fields on one object build a many-to-many relationship. For now, remember that the choice of type changes how deletion, ownership, sharing, and summaries behave.
Quick check
The Contact-to-Account link is a standard relationship. You can see the field in the Object Manager under Contact, then Fields & Relationships. Look for Account Name, which is the label of AccountId. The same screen is where you create your own relationship fields in later chapters.
Exercise
A custom object Invoice__c has a field Customer__c that points to Account. Which object is the child, and which relationship name would you use in a SOQL query to read the account's name from an invoice?
Show answer
Invoice__c is the child because it holds the relationship field. From child to parent you use the relationship name Customer__r, as in SELECT Customer__r.Name FROM Invoice__c.