Saturday, May 30, 2020

Many-to-Many Object Relationship (Using junction object) in Salesforce


Let's learn about many-to-many relationship between the objects in the Salesforce and how to feasibly depict it on the user interface.
 
In case of one-to-many relationship, we can define the relationship field (either lookup or master-detail) on the many side, that is on child object. we can see the list of child objects in the "related Lists" section of the parent object.

In case of many-to-many relationship, we don't have the option to create the relationship field as we have many-side at both the ends. Here comes the concept of junction object. It is nothing but a normal object that acts a child object and it has two master-detail relationship fields, each for both the many-side objects in question.

Let's understand it with an example. 

We have two objects named Student and Class in our org. As, a student can be enrolled in many classes (like Science, English) and a class can have many students, there exists a many-to-many relationship between these two objects. we will create a junction object to establish many-to-many relationship between student and class objects.

The schema for the above scenario will be as per the below image.
Schema diagram

We will create two master-detail relationship fields on the junction object and relate student & class objects with the junction object as per the above schema. you can give any name to the junction object as it is a normal custom Salesforce object.

How to represent it on user interface.

As the junction object is the child for both the student and class object, it will be displayed in the "related lists" section of both the objects. we have to modify the page layouts for student and class objects in order to see the relationship between them from the "related lists" section of each.

For that, find the "class" object from the object manager (1), click on page layouts (2) , go to the related lists section (3) and click on the "wrench" icon. Refer below image.

 
Object Manager image

You can add any of the student object fields in the "selected fields" section. For example, I have selected "Student Id" field. Click "OK" and save the layout.

 
Related List Edit Image

The selected field will be visible from the "Related Lists" section of the class object. Perform the same steps for the student object layout and select any of the class fields, so that it will be visible from the "Related Lists" section of the student object. 
 
Student Related Lists Image

Class Related Lists Image

we can see that student 1 is enrolled for c-1 and c-2 classes and class-1 has two students 1 and 2.

Important points about the junction object:

  • The first master-detail relationship you create on the junction object, will be the primary master-detail relationship and that master object will be the primary master object for the junction object.
  • Junction object inherits the owner field and look & feel from the primary master object.
  • Junction object inherits the security and sharing setting from both the master objects.

That's all about the "many-to-many relationship using junction object". Let me know your concerns in the comment section and please share the post if you find it helpful ! 



No comments:

Post a Comment