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

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.

Posted

in

by

Tags:


Related Posts

Comments

38 responses to “Deep dive into Junction object – Create many to many relationship in Salesforce”

  1. […] Check this article on blog to create many to many relationship in detail. […]

  2. Arun Singh Avatar
    Arun Singh

     clear and detailed explanation

  3. sailesh Avatar
    sailesh

    very detailed explanation

    thanks

  4. Prakash Avatar
    Prakash

    Awsome thanks a lot

  5. kiran Avatar
    kiran

    hi i have one question …….how to count more than 90k records in salesforce

    1. murali Avatar
      murali

      or you can use standard set controler that mesna use database.querylocator()

  6. rajkumar Avatar
    rajkumar

    Good Explaination.

    Anyone looking for salesforce jobs visit this site for latest jobs:

    http://salesforcejobsearch.blogspot.in

    1. Sridhar Sfdc Avatar
      Sridhar Sfdc

      Hi,
      Can you please explain me many to many relation ship with standard objects.
      Thank you in advance

  7. Kumar Avatar
    Kumar

    Thank You , Good explanation

  8. Smijith Nellikka Avatar
    Smijith Nellikka

    Junction object concept is clearly explained here..
    Thanks alot

  9. Piyush Avatar
    Piyush

    simple and nice explanation

  10. sai vasudha Avatar
    sai vasudha

    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?

    1. JitendraZaa Avatar
      JitendraZaa

      Yes sai, if you delete primary then secondary relationship will become primary…

    2. akash karhale Avatar
      akash karhale

      after delete any master ,child is delete so there is no consideration about priority remain..

  11. Naveen Avatar
    Naveen

    Fabulous explanation…

  12. Anujit Avatar
    Anujit

    very useful

  13. Sraboni Avatar
    Sraboni

    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. Praveen. Avatar
      Praveen.

      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 🙂

  14. Amit Avatar
    Amit

    Excelent explanation.

  15. Ashna Avatar
    Ashna

    What is the reason for creating 2 master-detail relationship instead of using 2 lookups?

  16. masood Avatar
    masood

    master detail relation ship using one record delete in parent but child record not delete

  17. priya Avatar
    priya

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

      1. Jitendra Zaa Avatar

        Thanks Nishant

  18. Lohith Matam Avatar
    Lohith Matam

    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

    1. Jitendra Zaa Avatar

      Hi Lohith, Child record will always follow permission of Primary Master.

  19. vineet Avatar
    vineet

    best content thank u Sir

  20. Nilesh Mendhe Avatar
    Nilesh Mendhe

    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. Jitendra Zaa Avatar

      Mostly Junction objects follow Mater Detail relationship, so security would be inherited from primary master.

      1. Prathibha Somavarapu Avatar
        Prathibha Somavarapu

        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.

        1. Shambo Ray Avatar
          Shambo Ray

          Same question from my end.can anyone highlight this?

  21. Rahul Avatar
    Rahul

    If I have a Juction object C having two Parents A & B with M-D Relationship. The OWD of A is Private and B is Public Read Only then what should be the OWD for C

  22. Amit Avatar
    Amit

    What Happen with Object B record If i delete the Object A Record [If A and B record are linked via object C]

  23. diariesofwonderwoman Avatar

    what will happen to junction object record(in this case C) if both A and B master records are deleted?

  24. Ayush Avatar
    Ayush

    Why there is a limit of only two master detail relationship per object?

  25. Sayak Avatar
    Sayak

    Best explanation on this topic.

  26. SANDIP Avatar
    SANDIP

    Thank you for detail explanation. I have one question here…why junction object inherited the security of primary object why not from secondary object. Is there any specific reason?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading