Deep dive into Junction object – Create many to many relationship in Salesforce

Article to show step by step process of creating junction object for Many to Many relationship in salesforce and impact of deleting one master record on others

In Interview for the salesforce technology, one common question asked is how to implement the many to many relationship. If we answer that using junction object then there next question comes how ? explain it and after that, next question will be what happen if i delete one master record ?

So, in this article i will first show you that how to create custom objects, how to relate them and what will happen if we delete master or child?

here i am going to assume that we want to relate A and B. Let us assume that we have two custom object – A and B.

To provide the many to many relationship between A and B, we will need to create one more object let’s say it would be C, it will be called as junction object.

So, start with creating Object A.

Creating Custom object in salesforce
Creating Custom object in salesforce

In same way, as shown in above image create B and C. For the simplicity of the example i have not added any field in Object A and B. By default Name field is provided by the salesforce on all objects.

We are going to use the object C as a junction object for A and B.

on object C, create “first Master-Detail” field and select object A as parent. This will be Primary Master – Detail relationship.

Salesforce Master-Detail relationship - select Master object
Salesforce Master-Detail relationship - select Master object

In next screen give the name of the field.

Salesforce Master-Detail relationship - Give the field Name
Salesforce Master-Detail relationship - Give the field Name

for the Master record A, i have used name “ChildOfA” and in same way for the Master record B – i used “ChildOfB”.

Untill now, we have created two fields on object C in which one refer to master record A and other will refer master record B as shown in below screen. Now we can create a tab for all the custom objects to add the record. First create record for A then B and for record C screen will look like below snap.

Salesforce Master-Detail relationship - Many to Many Relationship
Salesforce Master-Detail relationship - Many to Many Relationship

Few important points (Interview Questions of salesforce ):

  1. One Object can have only two Master-Detail relationships.
  2. If we delete record A (First Master detail relationship is always primary)  – then child record c will be deleted.
  3. If we delete record B then in this case also child record C will be deleted.
  4. If we delete record c then only C will be deleted , master record will not be deleted.
  5. If child C has two Master record A and B, Where A is primary relation then Child record C will inherit the look and feel of Parent object A.

Related posts

31 thoughts on “Deep dive into Junction object – Create many to many relationship in Salesforce”

  1. thanks for detailed explanation.
    but what happens to secondary master-detail relationship if we delete primary master-detail relationship that means will it automatically become primary master-detail relationship?

  2. Thanks for the nice explanation.
    But If one of the two objects already contains 2 master child relationship then how can I create a many to many relationship?
    Please explain.

    1. Hi Sraboni Here you are creating master child relation ship in child object C. Every time Master Detail Relationship is created in the child object. This is a new object created particularly to establish Many to Many relation with A and B only. No matter if A and B have Master Detail Relationship with other objects. Hope this will help you 🙂

  3. Hi,
    One doubt.. Many to many relationship can be created thru lookup as well. why we need a master details for junction object.

  4. u r fine with the topic as always but what about security of child object in case if the primary master has public read-only and secondary master has private

  5. Thank you for detailed information.
    Related Question : Who’s properties/security/OWD protocols will be inherited in Junction object.
    If ‘A’ is private and ‘B’ is public in OWD, what’s ‘C’ gonna be ???

      1. That’s a very clear explanation.For above scenario which is asked by Nilesh, the access for detail record remains private, if any of the primary (or) secondary master is having OWD private. Can you please confirm this one for me?

        I am able to access the child record as follows :

        i) If any one of A(or) B is “private” and other one “public” then C remains “private”
        ii) If any one of A (or) B is “Public Read Only” and other is “Public Read/Write” then C remains “Public Read Only”
        iii) If both A & B is “Public Read/Write” then C remains “Public Read/Write”

        Please shatter some light on this one.

Leave a Reply

Your email address will not be published. Required fields are marked *