Types of Relationships in Salesforce

AMIT SINGH
7 min readSep 23, 2022

--

INTRODUCTION

Data Model is very important when we are working on any project. To Understand the data model we first need to understand about what are the relationships in Salesforce and how one object can be associated with another object.

In this blog post, we will talk about what are the various relationships in Salesforce.

TYPES OF RELATIONSHIPS IN THE SALESFORCE

If we include the Polymorphic relationship then there are total 8 types of relationships in Salesforce

  1. Lookup Relationship
  2. Master-Detail Relationship
  3. Many To Many Relationship
  4. Hierarchy Relationship
  5. External Lookup Relationship
  6. Indirect Lookup Relationship
  7. Self Lookup Relationship
  8. Polymorphic Relationship

1. LOOKUP RELATIONSHIP

The lookup relationship is a very common relationship that you will be seeing in your Projects.

A lookup Relationship is a loosely coupled relationship that means the Parent record is not required on the Child Object.

Lookup relationship is used to relate the objects in 1:1 or 1:M ( one to many) fashions.

Note: — The Relationship field will always be on the Child Object.

In the above image, there are 2 objects (Contact & Case) that are related to the lookup field.

The contact record is the parent record and the Case is the child record. So the Lookup field will always be on Case Record. ( This is a standard lookup field ).

In the case of a lookup relationship If we delete the parent records then child records will stay in the system and vice versa.

Properties of Lookup Relationship

  1. The Parent Record is Optional on the child record.
  2. Roll-up summary fields are not available
  3. A standard object record can be on the detail side of a custom object in a lookup relationship.
  4. You can have a child record without a parent.
  5. You can have a maximum of 40 lookups on an object.
  6. By default record ownership of child, records are not controlled by the parent.

2. MASTER-DETAIL RELATIONSHIP

A very important and dangerous relationship in Salesforce is known as Master-Detail Relationship.

Like the lookup relationship, this is also used to create the 1:1 or 1:M (one to many) fashions.

In the case of a master-detail relationship, the master field is always required on the child object.

If we delete the parent record then the associated child record will also get deleted and if we undeleted the parent record using Recycle bin then the child records will also get restored.

When the relationship between 2 objects is a Master-detail relationship, then the child object will not have any owner which means if a user has access to Parent Record then the user will be able to access the child records as well no matter which user has created the child records.

From the above statement, we can conclude that the child record inherits all the security from the parent object.

In the below example, We have 2 objects Invoice & Invoice Line Item.

Let’s take an example here when you go to the market to buy something once you purchased some items say 3 items.

After billing you will get an Invoice and that Invoice will have Invoice Line Items in it.

So the relationship would be master detail. So If you throw(delete) the Invoice into the garbage you will not see the detail of the Invoice Line Items.

In Master-Detail Relationship, we can create the Roll-up Summary field on the Parent object which will be helpful for performing some aggerations such as SUM, Count, MIN & MAX related to Child Object.

For Example, counting the total no of Line Items under Invoice Record.

Properties of Master-Detail Relationship

  1. The Parent Record is required on the child record.
  2. Roll-up Summary fields are available on the parent record only.
  3. You can not create a Master-Detail Relationship field on Standard Object. For example, if You want an account object as a child side on any custom object or standard object then you can not use an M-D relationship. It will always be a lookup
  4. You can have a maximum of 2 master-detail on a child object.
  5. The child object can not have any owner which means parent record access determines the child record access.

3. MANY-TO-MANY RELATIONSHIP

Before we talk about the many-to-may relationship, let’s talk about an example here.

You have a custom object called Notifications where you create the various Notifications. Now you want to associate these notification records with users.

The catch here is that the same notification record can be assigned to multiple users and the same user can be assigned to multiple notification records.

So, how you will achieve this?

If you create the lookup or master-detail relationship with user on Notification Object then you will end up having duplicate records.

So, to overcome this issue Salesforce came up with the concept of Junction Object. Junction objects are used to achieve many many relationships.

And we can achieve the Many-To-Many relationship using Junction Object which will have 2 Masters with M-D Relationships.

To Know about Junction Object Click Here.

In the above example, One Notification Record can be attached to multiple Users and one User can be associated with multiple Notification Records.

4. HIERARCHY RELATIONSHIP

This is a special type of relationship which is only available on the User object and can only be related to the User Object.

This relationship is used to maintain the hierarchy of the user object.

5. EXTERNAL LOOKUP

External lookups are used to connect with external objects ( External Objects are objects out of Salesforce Org).

For Example, you are having friendships with people outside of your organization, college, or city.

In the case of Salesforce, we can consider relationships with external orders, invoices, and payments.
Read More: External Objects in Salesforce

6. INDIRECT LOOKUP

Indirect Lookup relations are used to connect External Objects with Salesforce Standard or Custom Objects.

To Create the Indirect Lookup Relationship with any Standard or Custom Object the parent object must need a field that is marked as Unique and External Id

7. SELF LOOKUP IN SALESFORCE

Self Lookup Relationship is used to connect the same object using Lookup relationship.

For Example, You have created an account(Company) record in Salesforce and that company is a child company.

In the above example, self-lookup will fit best.

Sometimes as a customer we raise a case to Salesforce Support and that case is similar to some other case that we raised so to relate these cases we can also use the lookup field on the Case object which is again a Self Lookup

POLYMORPHIC RELATIONSHIP

Polymorphic relationships are used when one child object can be related to multiple parent objects. We can not create the Polymorphic relationship on our own.

However, there are some standard objects which are using this relationship. Those objects are

  1. Task
  2. Event

Mostly we use polymorphic relationships in Salesforce SOQL Query.

For example, you wanted to list all the events related to account and Opportunity. So we will write the query as below

List<Event> events = [SELECT Description FROM Event WHERE What.Type IN ('Account', 'Opportunity')];

In the above query What.type will be referenced to the Parent Object API Name.

SUMMARY

Irrespective of a role in the Salesforce Industry, it is very important to know the core of Relationships in Salesforce.

I have tried to explain all the relationships that are present in Salesforce.

--

--

AMIT SINGH
AMIT SINGH

Written by AMIT SINGH

Software Developer by day & YouTube by night. Salesforce Architect || Salesforce MVP || Integration Expert

No responses yet