What is a Master-Detail relationship in Salesforce?

Overview

A Master-Detail relationship is a tightly coupled parent-child relation between two Salesforce objects. It enforces strong data integrity, sharing, and roll-up behaviors where the parent (master) controls key aspects of the child (detail) records.

Key characteristics

– The detail record must have a parent — the relationship field is required.
– Deleting the master deletes all related detail records (cascade delete).
– Detail records inherit the owner and sharing settings from the master; detail records do not have a separate owner field.
– The master can have Roll-Up Summary fields that aggregate data from related detail records (SUM, MIN, MAX, COUNT).
– You can enable reparenting (allow detail records to be reassigned to a different master) by checking the Reparent option when defining the relationship.
– An object can have up to two master-detail relationships (commonly used to create many-to-many junction objects).

When to use Master-Detail

Use Master-Detail when you need:

– Required parent/child data integrity (child cannot exist without parent).
– Roll-up summary fields on the parent to aggregate child data.
– Child records to inherit security and sharing from the parent.
– Cascade delete behavior.

Master-Detail vs Lookup relationship

– Required vs optional: Master-Detail enforces required parent; Lookup can be optional.
– Cascade delete: Master-Detail cascades delete; Lookup does not (unless configured via Apex/Flow).
– Sharing & ownership: Detail inherits owner/sharing from master; lookup records keep their own owner and sharing.
– Roll-up summaries: Only available for Master-Detail relationships.

Important limitations & considerations

– Before converting an existing Lookup to Master-Detail, all existing child records must have a value in the lookup field (no nulls).
– You can have at most 2 master-detail relationships on a single object.
– If Master-Detail is used to model many-to-many relationships (junction object), both relationships are Master-Detail on the junction object.

Example SOQL & Apex usage

Assume Invoice__c has a Master-Detail relationship to Account (Account is the master). Example SOQL to get the master name from the detail:

SELECT Id, Name, Account__r.Name FROM Invoice__c WHERE Account__r.Name = 'Acme Corp'

To create a roll-up summary on Account to calculate the SUM of Invoice__c.Amount__c, create the roll-up on the Account object and select the Invoice__c relationship and SUM on Amount__c.

Checklist before using Master-Detail

– Ensure child records should never exist independently of the parent.
– Confirm need for roll-up summary fields or shared ownership.
– Check data migration: populate parent field values for existing child records before conversion.
– Verify the requirement for up to two master-detail relationships if building a junction object.

Summary

Master-Detail is a powerful relationship type in Salesforce for enforcing data integrity, aggregating child data using roll-up summaries, and centralizing sharing and ownership. Choose it when parent-led control and cascade behavior are required; otherwise, use Lookup for looser coupling.