Master detail relationship salesforce is one of three core relationship types that enable powerful data modeling and queries in Salesforce. Understanding object relationships is essential for building scalable org architecture and creating efficient SOQL queries across related records.
Salesforce relationships allow you to connect objects and build complex queries to fetch related data. In Force.com, data can be abstracted by creating relationships between objects. A relationship field stores the parent record ID in a pre-defined relationship structure. Salesforce supports three relationship types: lookup relationship, master-detail relationship, and many-to-many relationship. For each relationship, you can specify up to 5 levels in a SOQL query.
Types of Object Relationships in Salesforce
- Lookup Relationship – Creates a simple foreign key relationship
- Master-Detail Relationship – Creates a parent-child relationship with cascading behavior
- Many-to-Many Relationship – Uses junction objects to connect multiple records
Master Detail Relationship Salesforce: Core Concepts
A master detail relationship salesforce creates a tight parent-child relationship between two objects. The parent object is called the “master” and the child object is the “detail.” This relationship type enforces data integrity and provides automatic behaviors that lookup relationships do not.
Key Characteristics of Master-Detail Relationships
- Required Relationship: Detail records cannot exist without a master record
- Cascading Delete: When a master record is deleted, all detail records are automatically deleted
- Security Inheritance: Detail records inherit sharing and security settings from the master
- Roll-Up Summary Fields: Master objects can display calculated values from detail records
- Ownership: Detail records are owned by the same user/team as the master record
Master-Detail vs Lookup Relationship
| Feature | Master-Detail | Lookup |
|---|---|---|
| Required Field | Yes | Optional |
| Cascading Delete | Yes | No |
| Security Inheritance | Yes | No |
| Roll-Up Summary | Yes | No |
| Ownership | Inherited | Independent |
Lookup Relationship in Salesforce
Lookup relationship creates a simple foreign key relationship that links one object to another. These relationships can create one-to-one and one-to-many relationships. When displaying records in the Salesforce UI, the platform generates a link to the related record, allowing navigation between related records.
Lookup Relationship Features
- Optional relationship field (can be blank)
- No cascading delete behavior
- Independent security and sharing rules
- Cross-object formula fields and validation rules supported
- Can reference up to 25 objects in lookup relationships
Many-to-Many Relationship Using Junction Objects
Many-to-many relationships in Salesforce require a junction object – a custom object with two master-detail relationships connecting the objects you want to relate.
What is a Junction Object?
A junction object is an intermediate custom object that creates many-to-many relationships between two parent objects. To implement a junction object:
- Create a custom object (the junction object)
- Add two master-detail relationship fields to different parent objects
- Configure page layouts and related lists for usability
Junction Object Considerations
- Usability: Users may need extra clicks to create records or navigate
- Ownership: Junction records inherit ownership from the primary master-detail relationship
- Deletion: Deleting either parent record deletes the junction record
- Reporting: Junction objects enable complex cross-object reporting
Salesforce Buyer Relationship Map: Business Use Cases
A salesforce buyer relationship map uses object relationships to model complex business scenarios. For example, connecting Accounts (companies), Contacts (people), Opportunities (deals), and custom objects (territories, products, partnerships) through various relationship types.
Common Business Relationship Patterns
- Account-Contact: Lookup relationship (contacts can exist without accounts)
- Account-Opportunity: Lookup relationship (flexible ownership model)
- Opportunity-OpportunityLineItem: Master-detail (line items require opportunities)
- Campaign-Lead/Contact: Many-to-many via CampaignMember junction
Best Practices for Object Relationships
Design Considerations
- Data Model Planning: Map business processes before creating relationships
- Relationship Limits: Maximum 2 master-detail relationships per object
- Performance: Avoid deep relationship queries (5+ levels) in high-volume scenarios
- Security Model: Consider how relationship types affect record access
Governor Limits and Performance
- SOQL queries can traverse up to 5 relationship levels
- Relationship queries count toward SOQL query limits
- Roll-up summary fields recalculate when detail records change
- Junction objects create additional database records
Common Relationship Errors and Solutions
Master-Detail Relationship Issues
- “Required field missing”: Detail records need master record ID
- “Cannot delete master record”: Delete detail records first or use cascading delete
- “Sharing rule conflicts”: Detail records inherit master security settings
SOQL Relationship Query Examples
// Query master records with detail information
SELECT Id, Name, (SELECT Id, Name FROM Contacts)
FROM Account
WHERE Industry = 'Technology'
LIMIT 100;
// Query detail records with master information
SELECT Id, Name, Account.Name, Account.Industry
FROM Contact
WHERE Account.Type = 'Customer'
LIMIT 200;
Frequently Asked Questions
What is the difference between master detail relationship salesforce and lookup relationship?
Master detail relationship salesforce creates a required parent-child relationship with cascading delete, security inheritance, and roll-up summary capabilities. Lookup relationships are optional, allow independent security settings, and do not cascade deletes. Master-detail is tighter coupling, lookup is more flexible.
Can you convert a lookup relationship to master-detail relationship?
Yes, but only if all child records have a value in the lookup field (no blanks). Salesforce will validate this requirement before allowing the conversion. Once converted to master-detail, you cannot change it back to lookup without deleting the relationship field.
How many master-detail relationships can an object have?
A custom object can have up to 2 master-detail relationships. Standard objects cannot be on the detail side of master-detail relationships. This limit exists because detail records inherit ownership and security from master records.
What happens to detail records when master record is deleted?
In master detail relationship salesforce, deleting a master record automatically deletes all related detail records (cascading delete). This behavior cannot be disabled. If you need to preserve detail records, use a lookup relationship instead.
How do object relationships affect SOQL query limits?
Relationship queries count toward your SOQL query limits (100 synchronous, 200 asynchronous). Each level of relationship traversal can impact performance. Queries spanning 5+ relationship levels may hit governor limits in high-volume scenarios.