What are the different types of Relationships in Salesforce?

Table of Contents

There are mainly 6 types of Relationships in Salesforce.

Lookup Relationships

  • It follows one-to-many relationship pattern, which means that many child objects can point to a single parent object. It is a loosely coupled relationship that allows you to connect one object to another.
  • In this relationship, a child object contains a field that contains a reference (‘looks-up’) to a record in the parent object.
  • Parent and child are independent of each other, which leads to no cascading deletion. Deletion the parent does not affect the child.
  • Child does not inherit sharing or security settings from parent, which makes it more flexible.
  • It establishes a unidirectional relationship, meaning that the child holds a reference to the parent object but the parent has no knowledge of the child object(s).
  • It can be made optional, meaning that the lookup field of the child object can be left blank without facing any error.
  • These are suitable for scenarios, where the relationship between records is not critical to their existence.
  • There can be a maximum of 40 lookups per object.
  • To create a lookup relationship, we have to select data type as lookup for the lookup field in the child object and select the parent object.

Example:

A Contact can have a lookup relationship to an Account. But the Contact and Account can exist independently. Deleting the Account won’t automatically delete the Contact.

Master-Detail Relationship

  • A tightly-coupled relationship, where the child object is dependent on the master object.
  • The parent is called the Master and the child is called the Detail.
  • It is a one-to-many relationship type, which means that one Master can have multiple Details.
  • The Detail cannot exist without the Master. If the parent record is deleted, the child record gets deleted, too (cascade deletion).
  • The master controls the behaviour of the child.
  • The sharing, security and accessibility settings of the parent are passed on to the child.
  • Master-Detail relationship allows the creation of roll-up summary fields in the master object, where we can aggregate data (sum, average, min, max, etc.) from the child record.
  • Validation and workflow rules can be defined that works across both the parent and child object.
  • An object can have a maximum of 2 Master-Detail relationships.
  • This is ideal for scenarios when we need the lifecycle of the child record to depend on the parent record.
  • To establish a Master-Detail relationship, we need to create a custom field in the child object with the data type Master-Detail and select the parent object.

Example:

Consider an object Expense Report and an object Expense. Expense Report is the Master object and the Expense is the Detail object. One Expense Report record can have multiple Expense records. If one Expense Report records is deleted, its corresponding Expense records will be deleted, too because of cascade deletion.

Many-to-many Relationships

  • Many-to-many relationship is defined as a relationship between two objects, where each record of one object is linked with multiple records of another object and vice versa.
  • Salesforce doesn’t have any direct way to link to objects in the form of many-to-many relationship.
  • We can implement many-to-many relationships using Junction Objects.
  • Junction Object is basically a custom object that has two master-detail relationships in it, one for each of the objects to be linked.
  • We can use it to link multiple records of one object to multiple records of another object.
  • Junction objects act as intermediaries between two objects.
  • These are very useful in scenarios where we need to link multiple records of one object with multiple records of another object, thus establishing a many-to-many relationship.
  • To establish many-to-many relationship, we need to create a junction object, which is a custom object that will link the records of the two objects. We need to create two master-detail relationship fields, where they will be details and the masters will be the two objects, one field will be linked to one object and the other to the other. Now, we have created the junction object where we can add records to establish many-to-many relationships.

Example:

Consider that we have two objects, Student and Courses. Multiple students could have taken multiple courses. So, to establish that, we can use a junction object called Enrollment. This would help us link multiple records in the Student object with multiple records in the Courses object.

Self-Relationships

  • Self-Relationships refers to the scenario where records from one object are linked to the record from the same object.
  • This is useful in scenarios where we want to create hierarchical or dependent relationships between records of the same object.
  • It is possible to establish self-relationships in any standard or custom objects.
  • This can be established by creating a lookup field in the object, which ‘looks up’ to another record of the same object.

Example:

Consider that you have an object named Company. You want to add a company as a record in it, which is the child company of a company record that is already present in the Company object. We can establish self-relationships in that case.

Hierarchical Relationships

  • Hierarchical relationship is a special kind of lookup relationship that is only available in the User object.
  • It represents an organization’s structure.
  • It is helpful in scenarios where we want to associate one user to another.
  • It allows us to create a lookup field that links one user to another, in a hierarchical structure.

Example:

Consider that you want to assign one user as a manager of another user. You can achieve that using hierarchical relationship, by linking one user to another.

External Lookup Relationships

  • External lookup relationships are referred to the relationship where a child Salesforce object, both standard or custom objects, or external object is linked to a parent external object, residing outside of Salesforce.
  • It is very useful in scenarios where we want to reference data from external sources directly into your Salesforce org.

Example:

Consider that you have a Salesforce object named Order that links to an external ERP system’s object named Product.

Indirect Lookup Relationships

  • Indirect lookup relationship refers to the relationship, where an external child object from and external system is linked to a Salesforce standard or custom object.
  • It is used when you need to integrate external data from an external system with Salesforce data without duplicating it.
  • It is done by creating a custom field in the Salesforce parent object by checking the ‘Do not allow duplicate values’ and ‘Set this field as the unique record identifier from an external system’ and mapping it with the external object field.

Example:

Consider that you have an external object in an external system named Invoice that references the Salesforce object Account via the unique external ID of the record in the Salesforce object.

Recent Post

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x