Master-Detail Relationship in Salesforce: A Step-by-Step Guide
What is a Master-Detail Relationship?
A Master-Detail Relationship is a strict type of association between two objects where the child record (detail) depends on the parent record (master). If the master record is deleted, the related detail records are automatically deleted, enforcing a clear hierarchy.
Key features of the Master-Detail Relationship include:
- Strict Parent-Child Dependency: Child records cannot exist without a parent.
- Ownership and Security Inheritance: The child record inherits the owner and sharing settings from the parent.
- Cascade Delete: When the master record is deleted, all associated detail records are also deleted.
- Roll-Up Summary Fields: You can create roll-up summary fields on the master object to calculate values from related detail records (e.g., totals, averages).
Use Cases for Master-Detail Relationships
- Invoice and Line Items: Each line item is dependent on its parent invoice. If the invoice is deleted, the line items must also be removed.
- Opportunities and Products: Products linked to an opportunity are dependent on the opportunity’s lifecycle.
- Campaigns and Campaign Members: Each campaign member is directly linked to a parent campaign.
How to Create a Master-Detail Relationship in Salesforce
Here’s a simplified step-by-step guide to creating a Master-Detail Relationship:
Step 1: Navigate to Object Manager
- Go to Object Manager in Salesforce Setup.
- Select the Detail Object (the object where you’ll create the relationship).
Step 2: Create a New Field
- In the Fields & Relationships section, click New.
- Select Master-Detail Relationship as the field type.
Step 3: Choose the Master Object
- In the Related To dropdown, select the object that will be the master (parent) in the relationship.
- Click Next.
Step 4: Configure Field Settings
- Set the Field Label and Field Name for your relationship.
- Define the sharing settings and whether the relationship field is required.
Step 5: Define Visibility and Layout
- Choose the profiles that can view/edit the field in Field-Level Security.
- Add the field to the relevant Page Layouts for visibility.
Step 6: Save the Relationship
- After completing the configurations, click Save to create the Master-Detail Relationship.
Key Differences from Lookup Relationships
- Dependency: Child records can’t exist without the parent in Master-Detail Relationships, unlike Lookup Relationships where the parent can be optional.
- Ownership: The child in a Master-Detail Relationship inherits the master’s ownership and security settings.
- Cascade Delete: Deleting the master automatically deletes the child records, while this is optional in Lookup Relationships.