How to implement many to many relationship in Salesforce | Junction Object in Salesforce

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now let us discuss the last type of relationship that is many-to-many relationship first of all let's understand what is the basics of many-to-many relationship work when there is a need to create a many-to-many relationship between two objects so for that I have taken a given example over here that is student Andrew so whenever a student like gets enrolled into a particular college or school there are different different groups into that college or school in which they can get enrolled so a single student can get enrolled into different different clothes and in a single group we can have different different shoe prints this way so this is a kind of this is basically many-to-many relationship which we denote using this now the question arises that on which object we need to create the foreign key or basically the relationship field because both are many and earlier we have understood in the other videos or in the previous videos that one to win a one-to-many relationship you need to create or less you feel on the many side but what happens or like what to do in many to many relationship both are many on which object we need to create the realistic field so let's see if we create the relationship field on the student object then let's see what the problem is so let's say I have created this relationship field over here and like let's all is include to mark is also in group two and Anthony is in group 3 and Paul is also on group 3 or group 4 then in that case I again have to rewrite this whole record of this whole row in this table and mentioned G 3 if he's into sports as well over here and this information will be repeated so again that's data redundancy repetition of the same data so we cannot create the field on this object now let's see that can be created mr. field on this group object if I'll create the relationship over here and let's say in group 1 we have student 1 in group do we have student 1 and in group 3 we have student 2 and over here but in group 1 we have student 3 as well so how to store that in these tables we can't do that I mean well I have to store it like this G 1 music Judy and rhythm and then we have to write down s2 or s3 who say our student is there in the G 1 group so this is again dinner in an NC which is not allowed which is not good so to create a many-to-many relationship where to create the feelings on this object or on this object the answer is none of them we need to create another table or another object in order to implement many-to-many relationship in any database or any relational database or instance and when it comes to Salesforce you need to create another object if you want to implement many-to-many relationship let me help you with that that how to create another object and using that how you can implement many-to-many relationship so here we are with another table which has two fields one will store the student ID and the other one will store the group ID what is the use of it let me explain it to you so this is basically a foreign key and this is the primary key and this is the primary key and this is the foreign key so to implement many-to-many relationship in a relational database we create a third table and in that third table we create two foreign keys one is related to the first object on the first table and the second one is related to the or ref the second one is referencing the primary key of the second object and the first one is referencing the primary key of the first object or the first table with the help of this real simply implement many-to-many relationship let me explain you how s1 is there in g1 so simply write down s1 and so we know that s1 is in Z 1 s1 is also in G 2 so we know that s1 is enrolled into Group 1 as well as group 2 now let's see that in group 1 we have s3 as well so Group one has two students s1 and s3 this is what you want to I mean this is exactly what we want many-to-many relationship established using a third table and in that third table we have two foreign keys which are referring or which are referencing the primary keys of both of these tables in salesforce this table is basically called as Junction object what this junction of they do is this this is simply used to create a many-to-many relationship between two objects it has two relationship fields one with the 1 or first object and the second one with the second object and the relationship that like happens is like this or the relationship that gets created will be like this so group has one-to-many relationship with this student in group table and student also has a one-to-many relationship with student and group table that means we create the foreign key or the relationship field on the many side object now in this kind of relationship many-sided this one with respect to student and many side is this one with respect to group so we'll create two relationship field on on an object to implement the master many-to-many relationship now I have a simple question for you tell me whether this relationship will be lookup or a master detail okay so no matter what your answer is these relationship fields will be master detail and let me explain you why in master Dell relationship whenever we delete the one side objects record the associated I love this regard or the many sand object records also get deleted so let's say if we believe s one student record or s one record then in that case both of these records will also get deleted and it makes sense as well that I mean if the students entries are not there and if students information is not there then what is the I mean notice the use of storing the information of in which group he was enrolled if there is no information of him available in the in the database and it is the same with group and student in group if you delete a particular group what is the use of storing the information that I mean which of the students who are enrolled into that particular group because if there is no information of that particular group what is the use of it I mean yeah you understand what I am trying to say hence both of these relationship fields will be master-detail and like hence this is the detail object for both of these relationships both of the marketing relationship fields will be created on student and group object and whichever object has to marshal relationship fields on to it will be called as junction object that's it so let's have a quick recap to implement a many-to-many relationship between two of this we first need to create a third object and onto that third object we need to create to modulate relationship fields which will be related to both of the objects between which we want to create a many-to-many relationship and the object on which we create too much relative fields is called as Junction object now there are some more things that you need to consider with many-to-many relationships and that are primary relationships and secondary relationship so let me give you a brief idea that what primary relationship is and what second relationship is when it comes to many to many relationship so whichever field you create first will become the primary relationship or will become the primary relationship field and with which object you create that field will become the primary master object so let's say that you created this field first on the student in group object so in that case student object will be called as primary master object and this will be called as primary relationship whereas if you have created this field first and like this group ID field of this relationship field first and in that case group will be called as a primary master object and this relationship will be called as a primary master relationship and like if you have created this relationship first and then this relationship then this will be called as a secondary relationship or this also can be called as a secondary master object that's it now there comes a question so in a mass shooting relationship the owner fields gets derived from the master object now over here this object has two masters so from which object over field owner field will get the right exactly the primary master object that is the reason we have understood the difference between primary master object and the secondary master object and what about the look and feel because detail objects in edit the look and feel of the master object as well that will again be derived from the primary master object and what about the security and sharing settings from which objects security and sharing settings will get derived or will be narrated this time you are probably wrong it's not only primary master object it's both of them primary master object as well as secondary master object and that means primary master primary relationship and as well as secondary relationships both control the security and sharing settings of the junction object that's it so let me give you an example to make you understand this if you're able to see g1 and you're not able to see s1 will not be able to see this record if you're able to see s1 and if you're not able to see g1 you'll not be able to see this records only if you are able to see both of these records then only you will be able to see this one that's it now there is an important question that I want to ask you guys how many relationship fields can we create on an object tell me it in two parts how many marshal militia fields can be created on a single object or an hour sorry and and and and and how many look of relation fields can be created on a single object tell me write it down in the comments and the first one who writes it down correct will get a new piece shorter so write down your answers in the comments and if you like this video or this relationship video series then in that case the like button and make sure you subscribe to the channel and you hit the bell I can as well and if I watching this video on LinkedIn or Facebook is a link down in the description this hit that link go to the Vita hit the subscribe button because you want to learn Salesforce and who else can teach you saying shows better than Salesforce al isn't it haha so who makes time what do I take care
Info
Channel: Salesforce Hulk
Views: 58,441
Rating: undefined out of 5
Keywords: Relationship in Salesforce, How to implement relationship in Salesforce, Create relationship in Salesforce, Understanding relationship in salesforce, Master Detail relationship, lookup relationship, many to many relationship, junction object, hierarchal relationship, self relationship, How to create junction object, what is the use of junction object, Why to create junction object, what is junction object, relationship field, Salesforce Object relationship
Id: vLUvpjx2soc
Channel Id: undefined
Length: 11min 10sec (670 seconds)
Published: Sat Oct 27 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.