Sub topic 6
Lookup, master-detail and many-to-many relationships
This chapter assumes you know the Salesforce Setup UI, understand foreign keys in relational data, and have seen fields and API names from earlier chapters. Relationships connect objects, standard or custom, so queries and reports can combine them.
How relationships work
A relationship is a special field on the child object that stores the Id of a record on the parent object. In relational terms, this field is a foreign key. The object that holds the field is the child. The object it points to is the parent.
Salesforce offers two main relationship types, and you build many-to-many links from them.
| Feature | Lookup | Master-detail |
|---|---|---|
| Parent required | No, unless you make the field required | Yes, always |
| Deleting the parent | Child stays, or deletion is blocked, depending on your setting | Children are deleted with the parent |
| Ownership and sharing | Child has its own owner and sharing | Child inherits the parent's owner and sharing |
| Roll-up summary fields on parent | Not available natively | Available |
| Reparenting | Always allowed | Allowed only if you enable it on the field |
Lookup relationships
A lookup is a loose link. The child record can exist without a parent, and each record keeps its own owner and security. Use a lookup when the two records have separate lifecycles.
In the Invoice example, Account__c is a lookup from the custom Invoice__c object to the standard Account object. In a query, you follow the lookup upward with the __r relationship name:
SELECT Name, Amount__c, Account__r.Name
FROM Invoice__c
WHERE Status__c = 'Open'The field Account__c holds the Id. The name Account__r lets you reach fields on the parent record.
Master-detail relationships
A master-detail link is a tight bond. The master is the parent and the detail is the child. Consider these rules:
- The detail record cannot exist without a master.
- Deleting the master deletes all its detail records.
- The detail record has no owner of its own. It inherits owner, sharing and security from the master.
- The master can have roll-up summary fields, such as the sum of
Amount__cacross all details.
If you add a master-detail field to an object that already has records, populate a lookup first, fill in every record, and then convert the field type. Converting fails if any child has an empty parent.
Many-to-many with a junction object
Salesforce has no direct many-to-many field. You build one with a junction object, which is a custom object with two master-detail relationships. Each junction record links one record from each side.
For example, a Course__c can have many students and a student can take many courses. The Enrollment__c junction object holds a master-detail field to Course__c and another to Contact.
The first master-detail field you create is the primary relationship. It sets the junction's look and feel and decides which master's owner and sharing it inherits. If you delete either master, its junction records are deleted too. If you delete the primary master, deletion follows the same rule, and the other master's records are untouched.
To list the courses for a contact, query through the junction:
SELECT Name, (SELECT Course__r.Name FROM Enrollments__r)
FROM ContactThe child relationship name Enrollments__r comes from the master-detail field definition. You can change it when you create the field.
Common errors
- Choosing master-detail too early. You can convert master-detail to lookup only if no roll-up summary fields depend on it. Plan the choice before you load data.
- Expecting a lookup to cascade deletes. By default, deleting a lookup parent clears the field on children. You can instead choose to block deletion.
- Wrong relationship name in SOQL. The parent direction uses the field's relationship name with
__r. The child direction uses the plural child relationship name. Standard relationships, such asContactson Account, have no suffix. - Forgetting sharing effects. With master-detail, a user who can see the master can see the details. Review security together with the model.
Exercise
A business needs to track which Projects use which Skills. Each Project needs many Skills and each Skill is used on many Projects. Which objects and relationship types do you create?
Show answer
Create a junction object, for example ProjectSkillc, with two master-detail fields: one to Projectc and one to Skillc. Each ProjectSkill__c record links one Project to one Skill.
Exercise
You want invoice totals on Account, but invoices must keep their own owners. Is master-detail the right choice?
Show answer
No. Master-detail makes invoices inherit the Account owner and removes their own ownership. Use a lookup, and calculate totals another way, such as a flow or Apex, or an add-on roll-up tool.