How to Create Master-Detail Relationship in Salesforce

In Salesforce, when we want to access records from one object to another then we use the relationship. In a relationship, we used parent-child terminology, so using that, we can say that we can access parent records into the child object.

In this Salesforce tutorial, we are going to learn about creating the master-detail relationship in Salesforce. In that, we will see what is master-detail relationship, when we can use it, and what are uses of MDR are in Salesforce.

Additionally, we will see step-by-step how to create a master-detail relationship in Salesforce Lightning and Salesforce Classic.

Master-Detail Relationship(MDR) in Salesforce

What is the master-detail relationship in Salesforce?

In Salesforce, relationships allow you to access data from one to another. We used parent-child terminology, so using that, we can say that we can access parent records into the child object. It is a strongly coupled relationship, which means it allows the parent to control the child’s records.

Characteristics of Master-Detail Relationships:

  • We create MDR on always many sides of an object. The MDR field is always required.
  • In MDR, the child object’s records don’t exist without the parent, which means when the parent records get deleted, then child records that associate with the parent are also deleted.
  • We can create only two MDRs per object. When we use this, we have the option to use the rollup summary field.
  • We can use the roll-up summary field on the parent object.

When can we use the master-detail relationship in Salesforce?

When we want to access the parent records in the child object or we need to apply the cross-object formula, then we can use MDR. When we want to enhance security between two objects, if parent records are deleted, then child records also should get deleted, then we can use MDR.

What are the uses of master-detail relationships in Salesforce?

Following are the uses of MDR in Salesforce:

  • It is a strongly coupled relationship.
  • The rollup summary field is available on using MDR.
  • The child inherits all the security and sharing from the parent object.

Important:

When you create the master-detail relationship, you should take care of whether we can create only MDR on that object in which records are not present. Otherwise, it gives an error.

See also  How to Create Field Dependency and Use in Salesforce

If there is any situation in that object already has records then first we need to create a Lookup Relationship and then convert it into a Master-Detail relationship.

You can read the whole article on how to convert a lookup relationship into the master-detail relationship: ……………………………

How to Create Master-Detail Relationship in Salesforce Lightning

Example:

Suppose you are working on a Custom Reimbursement_c object we want to create an application for any employee who has spent money for some reason, while creating this record you need to attach this record to the particular employee who spent the money.

This MDR connects two objects, and we can access the Employee_c object’s records in the Reimbursement_c object.

Using the above example and the following steps, we can create MDR in Salesforce Lightning.

1. As you log in to Salesforce.org, on the Home Page, you will see the Object Manager tab. Click on it, and in the search option, search for the object that you want to create the Maste-Detail Relationship. Here, I have selected the Reimburesement_c custom object. Click on that object.

Master detail relationship example in Salesforce

2. To create the new field, we have the option New; click on that option.

Create Master detail relationship on Object in Salesforce

3. Then you can see in the Data Type list you have the field Master-Detail Relationship. Click on it, then click on the Next button to set the next steps.

Apply Master detail relationship in Salesforce Lightning

4. Here, you need to select Related Object, in that you can select another object with which you want to create a relationship. This will be the Parent object of this object. Then click on the Next button.

Related object in master detail relationship in Salesforce

5. Here you can set a Field Label, this label will display on record details. I have entered Employee as a label. The Field Name automatically populates this will API name. Then click on the Next button.

Example of master detail relationship in Salesforce

6. In the Establish field level security for reference field section, we give Visibility to the Profiles. As the MDR field is required, it, by default, can be visible to all profiles. Here, we just click on the Next button.

Apply Master detail relationship in Salesforce

7. In the Related List Label, you need to give which name you want to display as a related list on the Record Page. After that, click on the Save button to create MDR.

How to create master detail relationship in Salesforce

8. You can see in the Field & Relationship option of the Reimbursement object that the Employee Field has been created with a Master-Detail Relationship.

Create Master detail relationship in Salesforce

9. Here we have created MDR on the Reimbursement object to check that go to the object. -> Click on the New button to create a record. -> You will see that the Employee field has been created, and it is required.

See also  Salesforce Workflow Rule Not Firing

When you click on that field the employee list has been popped up, and you can select employee from that list.

Master detail relationship in Salesforce Lightning

This is how we can create the master-detail relationship in Salesforce Lightning.

How to Create Master-Detail Relationship in Salesforce Classic

Example:

Let’s consider you are creating a new Manager record for your company with the custom Manager_c object, and while creating the records, you need to assign HR to every manager. It’s mandatory to save the records.

We can achieve this task by using a master-detail relationship, which allows us to access HR object records in the Manager object.

Using the above example and the following steps, we can create MDR in Salesforce Classic.

1. If you are in Salesforce Lightning then to switch to Salesforce Classic click on the View Profile which is top of the right side, you will get the option that Switch to Salesforce Classic.

In Salesforce Classic, on the Setup page, you will see the + Plus icon in the right corner; click on that icon. As you click on the + Plus icon, you will see All Tabs displayed on your screen. From these Tabs, click on the Manager Tab.

Master detail relationship examples in Salesforce Classic

2. After that you will see an arrow on the right side of the screen, click on that arrow and you can see the View Fields option. Click on it.

Example of MDR in Salesforce Classic

3. To create the new field in Custom Fields & Relationship we have the New Button, click on that button.

Master detail relationship example in Salesforce Classic

4. Then you can see in the Data Type list you have the field Master-Detail Relationship. Click on it, then click on the Next button to set the next steps.

Master detail relationships examples in Salesforce Classic

5. Here, you need to select Related Object in that you can select another object with which you want to create a relationship. This will be the Parent object of this object. Then click on the Next button. Here, I have selected the HR (Human Resource) object.

How to create master detail relationship in Salesforce Classic

6. Here you can set a Field Label, this label will display on record details. I have entered Assigned HR to Manager as a label.

See also  Salesforce Currency Field

The Field Name automatically populates this will API name. Then click on the Next button.

Example of master detail relationship in Salesforce Classic

7. In the Establish field level security for reference field section, we give Visibility to the Profiles. As the MDR field is required, it, by default, can be visible to all profiles. Here, we just click on the Next button.

Create Master detail relationship on Object in Salesforce Classic

8. In the Related List Label, you need to give which name you want to display as a related list on the Record Page. After that, click on the Save button to create MDR.

Create Master detail relationship in Salesforce Classic

9. You can see in the Field & Relationship option of the Manager object that the Assigned HR to Manager Field has been created with a Master-Detail Relationship.

Apply Master detail relationship in Salesforce Classics

10. Here, you can see we create a new record on the manager object. Assigned HR to Manager is required, and when we click on the lookup icon, the lookup dialogue will pop up, and from that, we can select HR and assign it to the manager.

Apply Master detail relationship in Salesforce Classic

This is how we can create the master-detail relationship in Salesforce Classic.

Conclusion

I hope you have got an idea about creating the master-detail relationship in Salesforce. In that, we have seen what is master-detail relationship, when we can use it, the characteristics of MDR, and what are uses of MDR are in Salesforce.

We also have seen which important points should be considered while creating the master-detail relationship. Additionally, we have seen step-by-step how to create a master-detail relationship in Salesforce Lightning and Salesforce Classic with examples.

You may like to read: