Relationships are always complicated either in our real life or in database example. A Master-Detail Relationship in Salesforce is quite similar to Lookup Relationship in which a Master or parent object controls the certain behavior of Detail or child object. In a Master-Detail Relationship (MDR), if the master (parent) record is deleted; the detail (child) record will also be deleted automatically. In this relationship, a Detail object can’t exist individually; it should be associated with a parent object. While creating a Master-Detail Relationship, a relationship field must be created on the detail object. Master-Detail Relationship is a Parent-Child Relationship where a Master object is considered as Parent while Detail object is considered as a Child object. In this article, we will discuss about basics of Master-Detail Relationship in Salesforce and How to Create a Master-Detail Relationship in Salesforce. Let’s start with the definition of a Master-Detail Relationship.
What is Master-Detail Relationship?
A Master-Detail Relationship is quite similar to Lookup Relationship where a Master or parent object controls the certain behavior of Detail or child object. A Master-Detail Relationship is used to model many-to-many fashioned relationships between two objects where each record of one objects is linked with multiple records of another object or vice-versa.
In a Master-Detail Relationship:
- Master- Parent
- Detail- Child
Example: A relationship between Product and offer is an example of a Master-Detail relationship. When we delete the Product object then offers associated with the product will automatically be deleted. Further, Account and Expense is also another example of a Master-Detail Relationship.
Similarly, in Amazon and Flipkart apps, a product shows different offers related to it but when this product is removed from the app then all offers attached with this product get automatically removed from the application.
Note: Standard object are not allowed as child object, only custom object are allowed as child object and a Master-Detail Relationship is always created when a custom object contains data.
Behaviors of Master-Detail Relationship
Similar to other Relationships in Salesforce, a Master-Detail Relationship is also used to derive values and to eliminate repetition and inconsistency in the database. There are certain behaviors that should be kept in mind before creating a Master-Detail Relationship in Salesforce.
- Once a Detail or Child record deleted, it moves to the recycle bin while the Master or parent record gets unaffected. Further, if Master record deleted, all detail (child) record gets automatically deleted.
- If you delete Detail record and then its linked Master record too separately, you can’t restore Detail record again as it no longer contains Master record linked with Detail record.
- You can’t reparent any record in a Master-Detail Relationship. As an Administrator, you can only reparented custom object’s child record to different parent records by selecting Allow Reparenting option in Master-Detail Relationship definition.
- The owner field is not available for the Detail or sub-detail records because by default the owner of Master record will be the owner of Detail record too.
- Sharing rules, manual sharing, or queues are not applicable for the Detail side custom object in a Master-Detail Relationship because these require the owner field.
- The Detail record doesn’t contain separate Security settings and Permission sets. These are the same as for a Master record in a Master-Detail Relationship.
- Master-Detail Relationship field must be available on the detail record page layout.
- The Master object can be standard as well as a custom object like Account, Opportunity, and Property, etc.
- The maximum child record for an ideal Master-Detail Relationship is 10000.
- There is a maximum of two Master-Detail Relationships and 25 relationships possible for each custom object.
- Once you save the relationship field, you can’t change Relate to option in a Master-Detail Relationship, so be careful when creating a relationship.
Create a Master-Detail Relationship in Salesforce
In this section, we will learn how to create a Master-Detail Relationship in Salesforce. Here, we want a master-detail relationship assuming Account is the master and contact is the detail object.
- Login to the Salesforce
- Click the Setup menu and then go to the Object Manager tab.
- Select a custom object and click on it.
- Click the Fields & Relationships tab on the sidebar.
- Click New
- Select Master-Detail Relationship and then click the Next button.
- Select property name (Case) for the Related to field then click the Next button.
- Enter the above-selected property for the field name and then click the Next button
- Click Next, Next, and Save.
Now, we have successfully created a Master-Detail Relationship for the case and favorites objects. If you look at the above image for the case record, you will find favorites is listed in the child relationship name tab.