Types of Object Relationships in Salesforce

Introduction

Understanding object relationships is fundamental for designing data models in Salesforce. Relationships define how records relate to one another, drive roll-up calculations, sharing behavior, and determine how you query data in SOQL and access related data in Apex and formula fields.

Primary Relationship Types

1. Lookup Relationship

A Lookup relationship creates a loose link between two objects. It is similar to a foreign key in relational databases. Key characteristics:

  • Parent and child records can exist independently.
  • No cascade delete by default (can be configured to prevent deletion if referenced).
  • Used when you need a simple reference without ownership implications.

SOQL example (child-to-parent):

SELECT Id, Name, Account__r.Name FROM Contact__c WHERE Id = '003...'

2. Master-Detail Relationship

Master-Detail is a tight coupling between two objects where the master controls certain behaviors of the detail:

  • Detail record inherits sharing and ownership from the master.
  • Deleting the master record cascades and deletes the detail records.
  • Roll-up Summary fields can be defined on the master to aggregate data from detail records.

SOQL example (master to detail via subquery):

SELECT Id, Name, (SELECT Id, Name FROM Opportunities) FROM Account WHERE Id = '001...'

3. Many-to-Many (Junction Object)

To model many-to-many relationships, use a junction object with two master-detail relationships to the two parent objects. The junction object acts like a bridge.

  • Junction object inherits sharing from both masters.
  • Enables relating one record of Object A to many records of Object B and vice versa.

Example: Course__c and Student__c linked via Enrollment__c (junction) with two master-detail fields.

4. Self Relationship

A object can have a lookup to itself (self-lookup). Common examples include a User manager relationship (User.ManagerId) or hierarchical relationships in custom objects.

5. Hierarchical Relationship

Special relationship available only on the standard User object for creating a manager-child chain. It’s a special lookup with UI and sharing considerations.

6. Polymorphic Relationship

Polymorphic lookups reference more than one object type. Examples include fields like WhoId and WhatId on Activity (Task/Event).

  • In SOQL, you can’t directly traverse polymorphic relationships using __r; use TYPEOF (in API versions supporting it) or check Id prefixes in code.

// Example TYPEOF (API versions that support it)
SELECT Id, Subject, TYPEOF Who
WHEN Contact THEN Name, Email
WHEN Lead THEN Name, Company
END
FROM Task LIMIT 10

7. External Lookup & Indirect Lookup (External Data Sources)

When integrating external objects (Salesforce Connect), two lookup types are available:

  • External Lookup — links a child external object to a standard/custom Salesforce object using an external ID.
  • Indirect Lookup — links a standard/custom Salesforce child record to an external object using an external ID on the Salesforce side.

8. External ID Relationships

While not a relationship type per se, External ID fields are frequently used to connect records from external systems (via upsert) and can be used in relationship lookups (by selecting external ID as the reference).

Key Differences: Lookup vs Master-Detail at a glance

  • Ownership & Sharing: Master-detail inherits ownership/sharing from master; lookup does not.
  • Cascade Delete: Master-detail cascades delete; lookup does not (unless configured).
  • Roll-up Summary: Only master-detail supports roll-up summary fields on the master.
  • Requiredness: Detail records must have a master; lookup fields can be optional (unless marked required).

Querying Relationships in SOQL

Use dot notation for child-to-parent (lookup/master-detail):

SELECT Id, Name, Account.Name FROM Contact

Use subqueries for parent-to-child (master-detail or lookup):

SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account

Accessing in Apex

In Apex, you can access parent fields on queried objects:

Account a = [SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account WHERE Id = :acctId];
for(Contact c : a.Contacts){
System.debug(c.Name);
}

Design considerations and best practices

  • Choose master-detail when you need ownership inheritance, roll-ups, or cascade delete; otherwise use lookup.
  • Use junction objects for many-to-many modeling and keep triggers and sharing implications in mind.
  • Avoid excessive deep relationship chains (performance and query complexity).
  • Use External/Indirect lookups when integrating with external systems via Salesforce Connect to minimize data storage duplication.

Conclusion

Salesforce provides flexible relationship types — lookup, master-detail, many-to-many via junctions, polymorphic, and external lookups — to model real-world data. Choosing the right relationship influences sharing, delete behavior, reporting, and how you write queries and Apex code.