Sub topic 9
Junction objects: a child of two parents
This chapter assumes you can create custom objects and fields in the Object Manager. It also assumes you know what a master-detail relationship is. A junction object builds on that.
The problem a junction object solves
A single relationship field links a child to one parent. That works when each child belongs to one parent, such as a Contact that belongs to one Account.
Some data does not fit that shape. A student can enroll in many courses, and a course has many students. Neither object can hold the link, because a single field can store only one parent Id.
A junction object solves this. It is a custom object that sits between two other objects and has two master-detail relationships. Each junction record represents one link between one record from each side.
Anatomy of a junction object
Take a school as the example. The two parent objects are Course__c and Student__c. The junction object is Enrollment__c, which has these fields:
Course__c, a master-detail field to Course.Student__c, a master-detail field to Student.- Optional fields that describe the link itself, such as
Enrollment_Date__corGrade__c.
Course and Student do not link to each other directly. Each is the parent in its own one-to-many relationship with Enrollment. Together, those two one-to-many relationships give you many-to-many.
The metadata for one of these fields looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!-- force-app/main/default/objects/Enrollment__c/fields/Course__c.field-meta.xml -->
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Course__c</fullName>
<label>Course</label>
<referenceTo>Course__c</referenceTo>
<relationshipLabel>Enrollments</relationshipLabel>
<relationshipName>Enrollments</relationshipName>
<relationshipOrder>0</relationshipOrder>
<reparentableMasterDetail>false</reparentableMasterDetail>
<type>MasterDetail</type>
<writeRequiresMasterRead>false</writeRequiresMasterRead>
</CustomField>The type value MasterDetail is what makes Course__c a master-detail field. The relationshipOrder value is covered in a later chapter.
What the master-detail fields imply
Because both fields are master-detail, the junction object inherits the behavior of that relationship type from each parent:
- Both parents are required. You cannot save an Enrollment without a Course and a Student. Both parent records must exist before you create the junction record.
- Deleting either parent deletes its junction records. Delete a Student and that student's Enrollments are deleted. The Course and its other Enrollments stay.
- There is no Owner field. Ownership and sharing come from the parents. A user needs access to both parents to work with the junction record.
- Both parents can have roll-up summary fields that summarize the junction records, for example the number of Enrollments per Course.
Junction records can carry data
The junction object is a normal custom object, so you can add your own fields. A field like Grade__c belongs to neither the student nor the course. It describes the enrollment, which is the link between them. If you find yourself wanting data about the link, a junction object is the right place for it.
Things to watch for
- A custom object can have at most two master-detail relationships. A junction object uses both of them.
- Duplicates are allowed by default. Nothing stops two Enrollment records with the same Course and Student. If that is a problem, add a uniqueness rule, such as a validation rule or a trigger.
- Create a junction object only for a real many-to-many need. If each child has exactly one parent, a plain lookup or master-detail field is simpler.
Exercise
A Student record is deleted. Enrollment is a junction object with master-detail fields to Student and Course. What happens to the Enrollment records and to the Course records?
Show answer
The Enrollment records that point to that Student are deleted, because deleting a master deletes its detail records. The Course records are not deleted. Only the Enrollments that linked them to the deleted Student are removed.
Exercise
You need to store each student's grade for each course. Which object should hold the Grade field, and why?
Show answer
Enrollment__c, the junction object. A grade describes one student in one course, so it belongs to the link between them. It does not belong to Student or to Course.