+ Start a Discussion
vajralavajrala 

What is junction object and how to create it?

i not understanding usage of junction object .

Please explain usage and how to create junction object with a example

Best Answer chosen by Admin (Salesforce Developers) 
Anup JadhavAnup Jadhav

Hi Vajrala,

 

Salesforce supports 2 kinds of relationships like Master Detail and Lookup. They are both one-to-many relationship, and they are both  defined from the many-to-one side, that is from a child to a parent. They can be made one-to-one relationship by adding validation rules, or maybe triggers to enforce the one-to-one nature, i.e. only one child is allowed.

 

Junction objects are used to create many to many relationships between objects. If you take the Recruiting application example, you can see that a Position can be linked to many Candidates, and a Candidate can apply for different Positions. To create this data model you need a third object "Job Application" that links the 2.

 

So you'd create a lookup field for both Position and Candidate object on the "Job Application" object. This will establish many to many relationship between Position and Candidate via the "Job Application" object known as the junction object.

 

Fore more information, read this article http://wiki.developerforce.com/page/A_Deeper_look_at_SOQL_and_Relationship_Queries_on_Force.com.

 

Anup

 

 

 

All Answers

Anup JadhavAnup Jadhav

Hi Vajrala,

 

Salesforce supports 2 kinds of relationships like Master Detail and Lookup. They are both one-to-many relationship, and they are both  defined from the many-to-one side, that is from a child to a parent. They can be made one-to-one relationship by adding validation rules, or maybe triggers to enforce the one-to-one nature, i.e. only one child is allowed.

 

Junction objects are used to create many to many relationships between objects. If you take the Recruiting application example, you can see that a Position can be linked to many Candidates, and a Candidate can apply for different Positions. To create this data model you need a third object "Job Application" that links the 2.

 

So you'd create a lookup field for both Position and Candidate object on the "Job Application" object. This will establish many to many relationship between Position and Candidate via the "Job Application" object known as the junction object.

 

Fore more information, read this article http://wiki.developerforce.com/page/A_Deeper_look_at_SOQL_and_Relationship_Queries_on_Force.com.

 

Anup

 

 

 

This was selected as the best answer
StaciStaci
Solution link no longer works.
JenniGiKi-DevJenniGiKi-Dev
Would you say, then, that the Campaign Member table, or a Contact Roles table, is a junction object?
tom1.3950299600768428E12tom1.3950299600768428E12
The Contract Role object is definintly a junciton object.  A bad one at that.  
Bruce Yue -- SF ArchitectBruce Yue -- SF Architect
Junction Object
A custom object with two master-detail relationships. Using a custom junction object, you can model a “many-to-many” relationship between two objects. For example, you may have a custom object called “Bug” that relates to the standard case object such that a bug could be related to multiple cases and a case could also be related to multiple bugs.
Creating the many-to-many relationship consists of:
Creating the junction object.
Creating the two master-detail relationships.
Customizing the related lists on the page layouts of the two master objects.
Customizing reports to maximize the effectiveness of the many-to-many relationship.
Creating the Junction Object
To create the junction object:
From Setup, click Create | Objects.
Click New Custom Object.
In the custom object wizard, consider these tips specifically for junction objects:
Name the object with a label that indicates its purpose, such as BugCaseAssociation.
For the Record Name field, it is recommended that you use the auto-number data type.
Do not launch the custom tab wizard before clicking Save. Junction objects do not need a tab.
Creating the Two Master-Detail Relationships
To create the two master-detail relationships:
Verify that the two objects you want to relate to each other already exist. For example, you may want to relate the standard case object to a custom bug object.
On the junction object, create the first master-detail relationship field. In the custom field wizard:
Choose Master-Detail Relationship as the field type.
Select one of the objects to relate to your junction object. For example, select Case.
The first master-detail relationship you create on your junction object becomes the primary relationship. This affects the following for the junction object records:
Look and feel: The junction object's detail and edit pages use the color and any associated icon of the primary master object.
Record ownership: The junction object records inherit the value of the Owner field from their associated primary master record. Because objects on the detail side of a relationship do not have a visible Owner field, this is only relevant if you later delete both master-detail relationships on your junction object.
Division: If your organization uses divisions to segment data, the junction object records inherit their division from their associated primary master record. Similar to the record ownership, this is only relevant if you later delete both master-detail relationships.
Select a Sharing Setting option. For master-detail relationship fields, the Sharing Setting attribute determines the sharing access that users must have to a master record in order to create, edit, or delete its associated detail records.
For the Related List Label that will display on the page layout of the master object, do not accept the default. Change this to use the name of the other master object in your many-to-many relationship. For example, change this to Bugs so users will see a Bugs related list on the case detail page.
On the junction object, create the second master-detail relationship. In the custom field wizard:
Choose Master-Detail Relationship as the field type.
Select the other desired master object to relate to your junction object. For example, select Bug.
The second master-detail relationship you create on your junction object becomes the secondary relationship. If you delete the primary master-detail relationship or convert it to a lookup relationship, the secondary master object becomes primary.
Select a Sharing Setting option. For master-detail relationship fields, the Sharing Setting attribute determines the sharing access that users must have to a master record in order to create, edit, or delete its associated detail records.
For the Related List Label that will display on the page layout of the master object, do not accept the default. Change this to use the name of the other master object in your many-to-many relationship. For example, change this to Cases so users will see a Cases related list on the bug detail page.
Monika JethiMonika Jethi
Anup,
"Job Application" object in Recruitng App is NOT a junction object as Junction object needs 2 Master Detail relationship (According to the documentation).
In above mentioned example it has 2 lookups , one on Position and other on Candidate.
VenkataSathwick SivvalaVenkataSathwick Sivvala
Go through the two links below.
https://help.salesforce.com/HTViewHelpDoc?id=relationships_manytomany.htm
http://www.jitendrazaa.com/blog/salesforce/deep-drive-in-junction-object-create-many-to-many-relationship-in-salesforce/
Jessica Miller 27Jessica Miller 27
I read that it has to be a master detail relationship and not lookups like Anup stated. Is this correct? 
Monika JethiMonika Jethi
Yes, that's true.. A junction object is a custom object with two Master-Detail relationships.
Dheeraj ChaudharyDheeraj Chaudhary
Hi Monika.

I have one junction object - suppose Test. And it creates two master relationship betweeen Account and Contact. All the relationship and records are presented in my QA environment. We have several recordsd like one contact associates with two account and vice versa. I have loaded my Account and Contact in DEV pro landscape using data loder, but as of now the relationship records are not present unless and util I load Junction Object Test. I have exported the data from Junction object but I dont know how to create the mapping files and to which I need to associate my ID's.
Account have external id - Account__c

Help me to load the data in DEV for Junction object
 
alfred smithalfred smith

Thanks body
that was very useful
Abhishek Honrao 10Abhishek Honrao 10
Thanks..
This answers are useful..
Hojoon ChoiHojoon Choi
I'm confused here.  A junction object is a junction between two other objects: Objects A, B, C where object B is the junction object to A and C.  Am I wrong?

Also, "A junction object is a custom object with two Master-Detail relationships." Does this mean is it a master AND a child so that it's A >B>C??  Because it can't be that this junctino object has 2 masters.  What am I not understanding here?
Hojoon ChoiHojoon Choi
I want to add that for some reason, my custom object does not allow for more than one master-detail field, which is why I was confused.  It's clear to me now that a custom object can have more than one master-detail field.
Laura BejjaniLaura Bejjani
I created a junction table between the case and a custom object called application user. I want to display all cases that relate to the application user on the case display related list. Is this possible and how would I go about doing that? I tried to create a relationship between case and case but I couldn't get that to work. 
Mounika BuddabathinaMounika Buddabathina
Hi Laura,
Yes,it is possible by realting the case(standard object) and application user(custom object which you created) by creating a junction object( In this case,Let's say Issues(custom object). Define a master-detail relationship field between case and issues,  application user and issues as well.
For more clear understanding,Check out this vedio.
https://youtu.be/Jy5JZ42XN6Q
Hope this is helpful.

Thanks,
Mounika.
Rahul-SharmaRahul-Sharma
A junction Object is one which is the detail of two master objects, not a lookup relationship.
ddddavoddddavo
So I hate to contradict everyone, but a junction object does not necessarily have to leverage master/detail relationships.  I know that's what many of the SFDC docs say, but the system doesn't force you.
In fact, I've built several SFDC orgs where the junction object uses only lookup relationships. You give up cascading deletes and rollup fields, but you get the ability to link to a third object...plus some interesting security features that aren't possible with the inherited ownership/visibility of master/detail.
farukh sk hdfarukh sk hd