Salesforce Menu

Relationship in Salesforce

You can imagine two tables are related in the database, they are linked together by a relationship field. This allows you to associate related information easily between tables. In a similar way Salesforce also supports relationship. All types of relationships maintain some form of a parent-child relationship. You create a relationship field in the child object (please, make sure relationship always travel from child object to parent object) In order to determine the parent, you need to think about which object is dependent on the other object and how many records of both objects could possibly be related to each other in your data model.

A relationship is an association among the objects. We can categorize the relationships into three kinds

  1. Self/ hierarchy
  2. One-to-Many
  3. Many-to-Many.

While Salesforce supports One-to-Many relationship out of box in 2 flavours as below:

  • Master-Detail Relationship
  • Lookup Relationship

Master-Detail Relationship:-

It is a tightly coupled relationship between Parent(Master) object and Child(Detail) object. What does Tight coupling means here:
   i. When you are deleting a parent record then the corresponding related record in the Child are also deleted (Cascading delete) automatically.

  ii. All the Security settings for the Child record are inherited from Parent which means, the same security settings specified for Parent are applied to Child records automatically.

  iii. Records in Child objects do not have Owner field, as these records are tightly coupled with Parent record. So the Salesforce Custom objects on Detail side cannot have Sharing Rules, Manual Sharing, and Queues which require Owner field.

iv. You can apply roll-up summery field only on a Parent(Master) Object in Salesforce.

Note: By default we cannot reparent the records in Child/Detail objects. If you want to allow reparenting the Child/Detail records, you should specify it when we defining the Master-Detail relationship by checking the “Allow Reparenting” checkbox as true.

Reparenting: Changing the Parent record of a Child record is called Reparenting in Master-Detail relationship in Salesforce.

Ex: Suppose Parent object have AB, XY records, and we created a child record for AB record, changing the parent from AB to XY in child object is called Reparenting. So the child record now associated with XY not AB

Other features:
  i. It is always defined on Child/Detail object
  ii. Master-Detail relationship is always a required field, hence always included in Edit page of child/detail object.
  iii. We cannot be removed from page layout as it’s a required field.
  iv. At most two Master-Detail relationships are allowed per child/detail object.
  v. Standard object cannot be at Child/Detail side when Custom object is on Master/Parent side.

Lookup Relationship:-

It is a loosely coupled relationship between Parent(Master) object and Child(Detail) object. What does Loosely coupling means here:
  i. When you are deleting a parent record then the corresponding related record in the Child are not deleted automatically.
  ii. All the Security settings for the Child record are not inherited from Parent which means, the different security settings specified for Parent as well as for Child records.
  iii. Records in Child/Detail objects have Owner field, as these records are loosely coupled with Parent records. So the Salesforce Custom objects on Detail side can have Sharing Rules, Manual Sharing, and Queues which require Owner field.
iv. You cannot apply roll-up summery field only on a Parent(Master) Object in Salesforce.

Other features:
i. We can make a lookup field required on page layout, if required.
ii. We can also make lookup field required when defining it.
iii. If the field is optional, in this case you can specify one of the following 3 actions to occur when parent record got deleted.
 a) Clear the value in lookup field (its default behaviour)
 b) Do not allow deletion of lookup record that’s part of lookup relationship.
 c) Cascade delete (this is available only by contacting Salesforce.com, and also only if the Salesforce custom object contains the lookup relationship, however the lookup object can be either Standard or
Custom).
 d) We can create a maximum of 38 lookup relationship fields per object if we have already created 2 master-detail relationships on same object.

Note: As per Salesforce a child/detail object can have at max 40 relationship fields onto it.

Let’s take an example of Master Detail relationship for our scenario:

In warehouse application we have Merchandise object and LineItem object where LineItem object is a child object to Merchandise object, as one record of Merchandise object comes multiple times in LineItem object.

Implementation tips: as we discussed earlier that this relationship is always travel from child to parent, so first go to lineItem object then apply Master Detail relationship to Merchandise object.

Example : from there go to “Custom Fields & Relationships” section then click on New button.

Step 1: Go to LineItem Object.

lineitem-object

Step 2: From there go to “Custom Fields & Relationships” section then click on New button.

Step 3: After clicking on new button, select Mater Detail Relationship filed and then clicks on Next button as below:

Step 4: After clicking on next button, you need to select the Parent/Master Object for LineItem Object as below:

master-object

Step 5: click on next until this filed got created.

New-relationship

Step 6: click on next until this filed got created.

New-relationship1

Step 7: click on next until this filed got created.

New-relationship2

Step 8: click on save button it will finally create a master detail relationship on LineItem Object.

New-relationship3

In Lightning Experience:  Relationship in Salesforce

Step 1. . Click from right top side in your screen and then click Setup

lightning-exp-create-app-salesforce

Step 2. From the Home tab in Setup, enter Object in the Quick Find box, then select Object Manager.

lightning-exp-object-manager

Step 3. Click Case as below

lightning-exp-object-case

Step 4. Click “Fields & Relationships” on object detail page

lightning-exp-fields-relationship-obj

Step 5. Click New on below screen

lightning-exp-fields-relationship-new

Step 6. Select the required data type and click Next button. Here we have selected Lookup Relationship.

lightning-exp-fields-relationship-case

Step 7. Select the Parent Object. We have selected Account as below and Click Next

lightning-exp-fields-relationship-account-case

Step 8. Populate Field label and Name and then click Next

lightning-exp-fields-relationship-account-case-relationship

Step 9. Select checkbox for profiles where we want this field to be visible and Click Next

lightning-exp-fields-relationship-account-case-field

Step 10. Select checkbox for Page layouts where we want this field to be added and Click Next

lightning-exp-fields-relationship-account-case-page-layouts

Step 11. Select checkbox for Page layouts where we want this Related List to be added and Click Save

lightning-exp-fields-relationship-add-to-custom-field

Step 12. Lookup Field created successfully.

lightning-exp-fields-relationship-case-created

Subscribe Now