Creating Master Detail relationship in Salesforce

Master Detail relationship is a one type of relationship which form tight bond relationship between Parent object and Child object. Here we call Master Object as Parent and Child object as Detail object.  Child records should always associated to a parent they can not remain orphan. Cascade record deletions happens means when a parent object record is deleted all it’s child records get deleted automatically. Standard Objects can not be a detail object(Child object).

Example:-  Account(Standard Object) can not be detail object.

The master-detail relationship fields is required on the page layouts of the detail object. Roll-up Summary fields can be created on Master object(Parent Object.) Child record inherits all security and sharing settings from it’s parent record.

Master Detail relationship in Salesforce Error.

When creating Master Detail relationship in Salesforce, an error will be displayed like “You cannot create a new Master-Detail relationship on an existing custom object if records already exist. You must first create a Lookup relationship, populate the lookup field with data in all records, and then change the relationship type to Master-Detail”. This is because a Master detail relationship does not allow for orphaned child records. Once if we have a lookup relationship with each child having  a parent, then change the field type to master detail relationship in child object.

ADVERTISEMENT

How to create Master Detail relationship in Salesforce.

Creating Master Detail relationship in Salesforce is very easy. Now we are going crate 1:m relationship between Pharma product and Samples. Here Pharma Product is Parent object and Samples is Child Object. Follow the steps given below.

  • Go to Setup | Build | Create | Objects.
Master Detail relationship in Salesforce

When we click on Object a list of custom objects will be displayed. Click on the child object to which you want to create Master-Detail relationship. As shown below

Master-Detail relationship in Salesforce

Now navigate to Sample object and click on it. Now a object definition page will be displayed. Now go to Custom fields and Relationships.

Master-Detail relationship in Salesforce

Now click on New button to create new relationship.

Step1:- Choose the filed type.

In Step 1 we have to choose the field type. Select Master-detail relationship as show below.

Master-Detail relationship in Salesforce

Click on Next between and choose the related object in Step 2.

Step 2 :- Choose the related object.

Master-Detail relationship in Salesforce

We have to select parent object as related object. Choose Pharma Object as related to as shown above. Now click on Next button.

Master Detail relationship in Salesforce

Click on Next button.

Master-Detail relationship in Salesforce

Click on Next button.

Master-Detail relationship in Salesforce
Master-Detail relationship in Salesforce

Click on Next button.

Now creating Master-Detail relationship between Pharma Product and Sample is is successful. Finally click on Save button to save all Settings.

Master Detail relationship in Salesforce

To check relationship between two object go to Child object(Samples). Click on Sample and create new record as shown below.

Master-Detail relationship in Salesforce

As there are no records in Sample click on New to create new record in Samples.

Master-Detail relationship in Salesforce

Enter the Quantity field.

Master-Detail relationship in Salesforce

And this is the relationship between Samples and Pharma Product. The relationship must always be on the child object. Here Sample is the child object and the relationship is master-detail relationship. Now we have to associate one Pharma product to Sample as shown above. Now click on Save button.

Master-Detail relationship in Salesforce

Now that we have successfully created record in Samples. Create two more records in samples.

  • Now click on Parent object(Pharma Object).
Master-Detail relationship in Salesforce
  • Go to vitamin D.
Master-Detail relationship in Salesforce

Now we see new related list are created. Before creating the relationship this related list are not presented here. After creating relationship only there is one to many relationship between Pharma product and Samples.

Master Detail relationship in Sfdc

Here the Pharma product (Vitamin D) is associated to two Sample records. This relation we call Master-Detail relationship. In Parent – Child relationship, the child record must be associated with parent. We can create Child Object records with out Parent object record. The Parent object record is mandatory in creating Child object record in Salesforce.

Conclusion

In this Salesforce tutorial we clearly understood about Creating Master-Detail relationship in Salesforce. In our next salesforce tutorial we are going to create about How to create Lookup relationship in Salesforce.