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.

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.

In next screen give the name of the field.

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.

Few important points (Interview Questions of salesforce ):
- One Object can have only two Master-Detail relationships.
- If we delete record A (First Master detail relationship is always primary) – then child record c will be deleted.
- If we delete record B then in this case also child record C will be deleted.
- If we delete record c then only C will be deleted , master record will not be deleted.
- 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.
 clear and detailed explanation
very detailed explanation
thanks
Awsome thanks a lot
hi i have one question …….how to count more than 90k records in salesforce
Have you tries @ReadOnly Annotation ? you can refer : http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content/pages_controller_readonly_context.htm
or you can use standard set controler that mesna use database.querylocator()
Good Explaination.
Anyone looking for salesforce jobs visit this site for latest jobs:
http://salesforcejobsearch.blogspot.in
Hi,
Can you please explain me many to many relation ship with standard objects.
Thank you in advance
Thank You , Good explanation
Junction object concept is clearly explained here..
Thanks alot
simple and nice explanation
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?
Yes sai, if you delete primary then secondary relationship will become primary…
after delete any master ,child is delete so there is no consideration about priority remain..
Fabulous explanation…
very useful
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.
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 🙂
Excelent explanation.
What is the reason for creating 2 master-detail relationship instead of using 2 lookups?
master detail relation ship using one record delete in parent but child record not delete
Hi,
One doubt.. Many to many relationship can be created thru lookup as well. why we need a master details for junction object.
Please check this post:
http://salesforce.stackexchange.com/questions/10166/why-does-the-documentation-stipulate-that-a-many-to-many-object-relationship-req
Thanks Nishant
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
Hi Lohith, Child record will always follow permission of Primary Master.
best content thank u Sir
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 ???
Mostly Junction objects follow Mater Detail relationship, so security would be inherited from primary master.
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.
Same question from my end.can anyone highlight this?
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
What Happen with Object B record If i delete the Object A Record [If A and B record are linked via object C]
what will happen to junction object record(in this case C) if both A and B master records are deleted?
Why there is a limit of only two master detail relationship per object?
Best explanation on this topic.
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?