Many 2 Many Relationship in Salesforce By Rajesh Chatterjee || Delipat.

Many to many relationship in Salesforce
Until now, we discussed lookup, master-detail and hierarchy relationships in Salesforce. We also created these relationships between outlet, revenue, head office and employee objects. In this article/video, we will discuss one to many and many to many relationships.
What is one to many relationship?
The relationship, where one record of the parent object can be related to many records of the child object, is known as one to many relationship. It is a simple relationship, so we need not create a separate field to establish the relationship.
For example, the relationship between outlet to revenue object and head office to outlet object is that of one to many relationship. It is because one outlet can have many revenues in the same way as one head office can have many outlets.
Let us know discuss many to many relationship
What is Many to Many relationship in Salesforce?
The relationship, where one record of an object can be related to many records of another object and vice versa, is known as many to many relationship.
For example, the standard objects lead and campaign are in many to many relationship. Multiple leads can be generated from a single campaign, and a single lead can be a part of multiple campaigns.
How to create many to many relationship in Salesforce?
In Salesforce, we do not have a separate data type to create many to many relationship. Therefore, we have to create a third object called a junction object to create many to many relationship between two objects.
What is a Junction object in Salesforce?
A junction object is a third object that helps to link two objects in many to many relationship in Salesforce. The only reason for creating junction objects is to facilitate the creation of many to many relationships. The junction object is always the child object of the other two parent objects.
Junction object can be in either lookup or master-detail relationship with the parent object based on the requirement of the business.
Steps to create many to many relationship in Salesforce
Let us learn the creation of many to many relationship with an example.
Consider the relationship between teacher and student objects. Many students can take lessons from one teacher, and many teachers can be teaching one student. Hence, teacher and student objects are in many to many relationship.
But, it is difficult to understand which student is learning from which teacher. The junction object of the many to many relationship helps us understand that.
To create many to many relationship, we have to first create three custom objects.
Teacher- one parent object
Student- another parent object
Class- Junction object
For easier understanding, create a custom app School and add all three objects to School app.
We have discussed the creation of custom objects and apps in previous articles/videos.
Then, let us add a few records in the objects.
We have two teachers- Alex and John and two students Rajesh and Rishik.
Since we have created the custom objects and added records to them. Let us now create a many to many relationship and establish it.
To create many to many relationship, we have to create two master-detail relationships in the junction object Class.
Go to Setup and click on object manager.
Go to the object detail page of the Class object.
Click on Fields & relationships on the sidebar.
Click on the new button.
Select the data type as master-detail relationship.
Select the teacher object as related to object.
Enter the details, field label and field name as teacher name. Add a description and help text.
Click next, next and save & new.
Again select the data type as master-detail relationship.
Now, select the student object as related to object.
Enter the details, field label and field name as student name. Add a description and help text.
Click next, next and save.
Many to many relationship is created between teacher and student object.
Now, let us verify the relationship by creating records in the class object.
Go to School app and click on the classes tab.
Click on the new button.
Enter the details.
Class name- Class 1 sec 1
Teacher name- Alex
Student name- Rajesh
Click save & new.
Enter some more records.
Whenever you create a record in the class object, you are making a many to many relationship. You can now see which student is learning from which teacher. A many to many relationship helps us understand this.

Пікірлер: 2

  • @victoryvitalis3473
    @victoryvitalis34733 ай бұрын

    Thanks

  • @tejasai9390
    @tejasai93902 жыл бұрын

    Crystal clear explanation, thanks for uploading the video!