Sub topic 1
How Salesforce Objects Relate: Lookup and Master-Detail
Prerequisites
This chapter assumes you already know:
- Basic SOQL syntax:
SELECT,FROM,WHERE,ORDER BY, andLIMIT. - That Salesforce data lives in objects, which have fields, and that each record has an ID.
- How to open Object Manager in Setup and view an object's fields.
You do not need to write Apex yet. Later chapters cover it.
Why relationships matter for queries
A relationship links two objects so that one record can point to another. A Contact points to the Account it works for. An Opportunity points to its Account. SOQL does not use JOIN. It follows these links instead. To write relationship queries, you first need to know how the links are built.
Every link is stored in a field on one object. That object is the child. The object the field points to is the parent. The field holds the ID of the parent record.
Contact.AccountIdholds the ID of an Account. Contact is the child and Account is the parent.- A custom field
Task__c.Project__cholds the ID of a Project.Task__cis the child andProject__cis the parent.
The parent-child direction decides which query style you use. Later chapters cover both styles.
Lookup relationships
A lookup relationship is a loose link between two records. The child has a field that can point to a parent record.
Key properties:
- The lookup field can be empty by default. A Contact can exist without an Account.
- Deleting the parent does not delete the child by default. The lookup field is cleared instead. You can configure the field to block deletion.
- The child has its own owner and its own sharing settings.
- Roll-up summary fields are not available on lookups.
Standard examples are Contact.ReportsToId and Case.ContactId. You can also create custom lookup fields.
Master-detail relationships
A master-detail relationship is a tight link. The master is the parent. The detail is the child.
Key properties:
- The master field is required. A detail record cannot exist without a master.
- Deleting the master deletes its detail records too.
- The detail record inherits sharing and ownership from the master. It has no owner of its own.
- The master object can have roll-up summary fields that calculate values over its detail records, such as a count or a sum.
OpportunityLineItem is a standard detail of Opportunity. Custom master-detail fields are created in Object Manager.
Comparing the two
| Feature | Lookup | Master-detail |
|---|---|---|
| Parent required | No, by default | Yes |
| Deleting the parent | Clears the field, or is blocked | Deletes the children |
| Child ownership and sharing | Child has its own | Inherited from the parent |
| Roll-up summary fields | Not available | Available on the master |
For SOQL, both types behave the same way. You traverse them with the same syntax. The differences affect your data. For example, a lookup child can have a null parent, so your code must allow for that.
Seeing the structure
The diagram below shows a small data model. Arrows point from the child to the parent.
An Account can have many Contacts, so one parent maps to many children. A child record has at most one parent through any single relationship field. An object can have several relationship fields, so a child can have several different parents.
Reading the model before you query
Before you write a query, answer three questions:
- Which object is the child, holding the relationship field?
- Which object is the parent?
- What is the field's API name? A custom field ends in
__c, such asProject__c.
You can find the answers in Object Manager under Fields & Relationships. The field's detail page shows the related object and the type of relationship. For standard objects, the Object Reference in the Salesforce documentation lists each relationship field.
Common mistakes at this stage
- Treating the larger object as the parent. Direction depends only on which object holds the field.
- Assuming every child has a parent. Lookup fields can be empty.
- Assuming a deleted parent leaves its children behind. Master-detail children are deleted with it.
- Expecting roll-up summary fields on a lookup. They need a master-detail relationship.
Exercise
A custom object Invoice_Line__c has a required field Invoice__c pointing to Invoice__c. Deleting an invoice must delete its lines, and invoice lines must share the invoice's access settings. Which relationship type fits, and which object is the child?
Show answer
Master-detail. A required parent, cascade delete, and inherited sharing are all master-detail properties. Invoice_Line__c is the child because it holds the relationship field. Invoice__c is the parent.
Exercise
A Contact record has an empty AccountId. Is this possible, and what does it tell you about the relationship type?
Show answer
Yes. The field is a lookup, which is optional by default. A master-detail field is always required, so its value could never be empty.