Sub topic 12
Lookup or Master-Detail, and Many-to-Many with Junction Objects
This chapter assumes you know what objects, records and fields are, and that you can create a custom field in Object Manager. It also assumes you have read the chapter on relationship fields. Here the focus is on choosing between the two main relationship types.
The core difference
A lookup is a loose link between two records. A master-detail is a tight link where the parent (master) controls the child (detail).
| Behavior | Lookup | Master-detail |
|---|---|---|
| Child record can exist without a parent | Yes, unless you make the field required | No, the parent is always required |
| Deleting the parent | Clears the lookup, or blocks the delete, depending on your setting | Deletes all child records |
| Record owner of the child | Child has its own owner | Child has no owner field and inherits the parent's owner and sharing |
| Roll-up summary fields on the parent | Not available | Available |
| Can be added to an object that already has records | Yes | Only after the field is first created as a lookup and every record has a value |
| Limit per object | 40 relationships (including master-detail) | 2 master-detail fields |
Choosing between them
Ask these questions in order:
- Must the child never exist without the parent? If yes, use master-detail.
- Do you need a roll-up summary on the parent? If yes, use master-detail.
- Should the child be shared and owned independently of the parent? If yes, use lookup.
- Does the object already hold records with no parent value? Use lookup, or fill in the values and convert later.
Master-detail also has a cost. Because the child inherits sharing and cascades deletes, a mistake in the parent can remove a lot of data. Use lookup when in doubt, since converting a lookup to master-detail later is possible but converting back requires that no roll-up summary fields depend on it.
Many-to-many with a junction object
Salesforce has no direct many-to-many field. You build one with a junction object, a custom object that has two master-detail fields, one to each side.
Example: a student can enroll in many courses, and a course has many students. Create an Enrollment__c object with a master-detail to Student__c and another to Course__c.
Each enrollment record links one student to one course. Both parents can show a related list of enrollments, and each can hold roll-up summaries, such as the number of students in a course.
The first master-detail field you create becomes the primary master. It decides the junction record's owner and sharing. If you delete either parent, the junction records under it are deleted too.
Common errors
- Choosing master-detail for convenience. Cascade delete and inherited sharing surprise teams later.
- Using a lookup on the junction object. You lose roll-ups and the shared lifecycle.
- Creating a master-detail on an object with existing data. Create a lookup first, fill in every value, then convert it.
Exercise
A Project object has many Task records. Tasks should be deleted when the project is deleted, and the project must show the total hours of its tasks. Which relationship do you use, and why?
Show answer
Master-detail from Task to Project. It deletes tasks with the project, and it is the only relationship type that supports a roll-up summary field (a sum of hours) on the parent.
Exercise
How do you model a many-to-many link between Contact and Event Session, and what decides who owns the junction records?
Show answer
Create a junction object with two master-detail fields, one to each parent. The first master-detail field created is the primary master, and it decides ownership and sharing of the junction records.