Introduction
In Salesforce, the Master-Detail Relationship is a crucial tool for establishing a tightly coupled relationship between two objects. It defines a parent-child relationship where the child’s existence depends on the parent.
This blog will walk you through the concept of Master-Detail Relationships, when to use them, how to create them, and a practical example.
What is a Master-Detail Relationship?
A Master-Detail Relationship is a strong bond between two objects in Salesforce.
The parent object (Master) controls the lifecycle of the child object (Detail), meaning that the child object cannot exist independently.
Key Features of Master-Detail Relationships:
- Parent-Dependent: The child record is dependent on the parent record.
- Cascade Deletion: Deleting the parent record automatically deletes the related child records.
- Roll-Up Summary Fields: You can perform calculations (sum, average, count, etc.) on child records and display the results in the parent object using roll-up summary fields.
- Ownership and Sharing: The child record inherits the sharing and security settings of the parent record.
- Mandatory Field: The relationship field is always required on the child object.
When to Use a Master-Detail Relationship?
Use a Master-Detail Relationship when:
- Dependency: The child record SHOULD NOT exist without a parent record.
- Data Aggregation: You need roll-up summary fields to calculate values across related records.
- Ownership Control: You want the child records to follow the same ownership and sharing rules as the parent.
How to Create a Master-Detail Relationship in Salesforce
Let’s create a Master-Detail Relationship between two objects: Invoice and Invoice Line Item.
Scenario:
An invoice (parent) can have multiple line items (child), but a line item cannot exist without an associated invoice.
Steps to Create a Master-Detail Relationship
Step 1: Create the Objects
- In Salesforce, go to Object Manager.
- Create a custom object named Invoice with fields like Invoice Number, Date, etc.
- Create another custom object named Invoice Line Item with fields like Product Name, Quantity, Price, etc.
Step 2: Add the Master-Detail Relationship
- Go to the Invoice Line Item object.
- Under Fields & Relationships, click New.
- Choose Master-Detail Relationship and click Next.
- Select Invoice as the parent object and click Next.
- Configure the field name (e.g., Invoice).
- Set field-level security and page layout options as needed.
- Save the field.
Step 3: Test the Relationship
- Create a few Invoice records (e.g., Invoice 001, Invoice 002).
- Create an Invoice Line Item record and link it to an Invoice.
- Try deleting an Invoice record and observe how its associated line items are automatically deleted.
Example in Action
Scenario:
You manage invoices and their associated line items in Salesforce. Each invoice contains multiple line items, but no line item can exist without an invoice.
1.Record Creation:
- Invoice: Invoice 001
- Invoice Line Items:
- Product: Product A, Quantity: 2, Price: $50
- Product: Product B, Quantity: 1, Price: $100
2. Roll-Up Summary Example:
Add a roll-up summary field to the Invoice object to calculate the total price of all line items. The roll-up summary field will sum up the Price fields of all associated line items.
- Total Price for Invoice 001: $200
3. Cascade Deletion:
If Invoice 001 is deleted, its associated line items (Product A and Product B) are automatically deleted as well.
4. Query Example (SOQL):
- To retrieve invoices along with their line items:
- This query fetches all invoices with their respective line items.
SELECT Name, (SELECT Product_Name__c, Quantity__c, Price__c FROM Invoice_Line_Items__r) FROM Invoice__c
Use Cases of Master-Detail Relationships
- Order and Order Items: Orders (parent) and their associated items (child).
- Project and Tasks: A project (parent) with multiple tasks (child).
- Campaign and Campaign Members: Campaigns (parent) and related members (child).
Advantages and Limitations of Master-Detail Relationships
Advantages:
- Roll-Up Summary Fields: Calculate aggregate values like sum, count, and average.
- Cascading Effects: Automatically manage child records when parent records are updated or deleted.
- Controlled Sharing: Child records inherit sharing settings from the parent.
Limitations:
- Dependency: Child records cannot exist without parent records.
- Limited Relationships: An object can only have two Master-Detail Relationships.
- No Independent Ownership: Ownership and sharing rules of child records are tied to the parent.
Conclusion
The Master-Detail Relationship is a powerful tool in Salesforce for tightly coupling related data.
By understanding its features and limitations, you can efficiently model real-world relationships like invoices and line items, projects and tasks, or orders and order items.
When designing your Salesforce schema, carefully evaluate whether the data dependency and aggregation needs justify a Master-Detail Relationship or if a Lookup Relationship would suffice.