Overview
Understanding object relationships in Salesforce is fundamental for modeling data correctly. Salesforce supports several types of relationships between objects—each with different behavior, sharing implications, and UI representation. This post explains the main relationship types, when to use them, and practical examples.
1. Lookup Relationship
A Lookup Relationship creates a loose link between two objects. Records remain independent — deleting the parent does not automatically delete the child (unless configured), and security & sharing are handled separately.
Use cases:
- Optional relationships (child may or may not have a parent).
- Linking objects without enforcing ownership or cascade deletes.
Example (Schema Builder or Metadata):
// Field type: Lookup
AccountId on Contact refers to Account
2. Master-Detail Relationship
A Master-Detail Relationship tightly couples child records to a master record. Key characteristics:
- Child record’s lifecycle depends on the master (deleting master deletes children).
- Sharing and security are inherited from the master.
- Roll-up summary fields are available on the master to aggregate child data.
Use cases:
- When child records must be owned/controlled by the master.
- When you need roll-up summary aggregations (SUM, COUNT, MIN, MAX).
// Example: OpportunityLineItem is a detail to Opportunity (master)
3. Many-to-Many Relationship (Junction Object)
Salesforce models many-to-many relationships using a junction object with two master-detail relationships to the two objects being related. This lets a single record relate to multiple records of another object and vice-versa.
Use cases:
- Relating Products to Campaigns, Courses to Students, etc.
// Junction object: Course_Enrollment__c with Master-Detail to Student__c and Course__c
4. Hierarchical Relationship (User object specific)
The Hierarchical Relationship is a special lookup available only on the User object and is used to create a manager hierarchy (User -> User).
Use cases:
- Modeling manager and reporting structures, approval chains, etc.
5. External Lookup and Indirect Lookup (Salesforce Connect)
Used with Salesforce Connect when integrating external data sources:
- External Lookup links a child external object to a parent standard/custom object using an external ID.
- Indirect Lookup links a child Salesforce external object to a parent using an indirect lookup via an external ID on the parent.
Use cases:
- Lightweight integration with real-time access to external records without copying data into Salesforce.
6. Polymorphic Lookup (WhoId/WhatId)
Polymorphic lookups allow a lookup field to refer to multiple object types. Classic examples are the WhoId and WhatId fields used on Activity (Task/Event):
WhoIdcan refer to a Contact or Lead.WhatIdcan refer to an Account, Opportunity, Campaign, Custom Object, etc.
Limitations:
- Cannot create custom polymorphic lookup fields (only platform-provided ones).
Comparison Summary
Key points to decide which relationship to use:
- Need roll-up summaries or cascade delete & inheritance of sharing? Use Master-Detail.
- Need optional or loose association? Use Lookup.
- Need many-to-many? Create a Junction Object with two master-detail relationships.
- Working with external data? Use External/Indirect Lookup via Salesforce Connect.
- Need a user hierarchy? Use Hierarchical Relationship on User.
- Need flexible reference to different object types? Use platform provided Polymorphic fields.
Practical Tips and Best Practices
- Prefer Lookup for flexibility; convert to Master-Detail only if you require roll-ups or ownership inheritance.
- Be cautious converting lookup to master-detail: if child records exist without a master, conversion can be blocked.
- Avoid excessive master-detail chains—complex sharing calculations can impact performance and maintenance.
- Document junction objects clearly to show intent (they represent many-to-many).
Sample SOQL & Apex Pattern
Querying related records (Lookup example):
List
Creating a junction object record (Many-to-Many):
Course_Enrollment__c ce = new Course_Enrollment__c(Student__c = studentId, Course__c = courseId);
insert ce;
Conclusion
Choosing the correct object relationship in Salesforce affects data integrity, sharing, reporting, and automation. Evaluate requirements for ownership, deletion behavior, aggregation, and integration to pick the right relationship type.








Leave a Reply