Salesforce provides multiple types of relationships in order for you to connect objects together in different ways.
You may be familiar with the two main relationship types: lookup relationships and master-detail relationships. These allow you to create a normal parent-child relationship, which is sometimes referred to as a 1-to-many relationship. This means there is only ever one parent, but multiple child records can be associated. However, what happens when you need to connect “many” parents to “many” child records? Let’s take a look…
Salesforce Junction Object
Salesforce Junction Objects give you a way to create a many-to-many relationship between Salesforce objects. They are created using a custom object to relate two other objects via two master-detail relationships. This is necessary when modeling certain data schemas. Let’s dive into a couple of examples:
Junction Object Examples
Take a recruitment process where you have a job position and candidate object. As it is a requirement for candidates to apply to multiple job positions, a standard parent-child relationship would not work. You need a junction object, called something along the lines of ‘Application’, to allow many candidates to apply to multiple job positions.
There is also an example within standard Salesforce objects with Opportunity Products. If you want to assign Products to a certain Salesforce Opportunity, you will need to use the Opportunity Line items object (a junction object). This is required as, without the Junction Object, you would only ever be able to associate one product to one opportunity.
Creating a Junction Object in Salesforce
Creating a junction object is simple, and once you’ve created your first one, the concept will become second nature. I’m going to use the recruitment process example from above to makes things simple:
1. Firstly, make sure that you have available the two objects you wish to connect with a junction object. I’m going to create two custom objects: Candidate and Job Position. As you can see from the Salesforce Schema Builder, these are currently disconnected. To ensure that many candidates can apply for many jobs, we need our junction object.
2. The next step is to create a new custom object that will serve as our junction object. I’ve created a custom object called “Job Application”, which will enable us to connect the Candidate and Job Position objects.
3. We now need to create our two master-detail relationships on the Job Application object – one will need to connect to our Candidate object, the other to Job Position. You can do this by creating a field (as normal) on your Job Application object, then navigating to the master-detail relationship field type, and selecting our other objects. If we go back to the Schema Builder, we can see our many-to-many junction object is complete!
From the above setup, we are now able to assign many candidates to many job positions. Let’s have a look at how this looks inside of Salesforce…
Salesforce Example
As you can see in the below image, the junction object has enabled more than one individual to apply for this Salesforce Admin position.
It also allows the individual to apply for multiple job positions within the company…
Summary
That’s all there is to it! Junction objects are a core element in understanding how the Salesforce data model works. It’s vitally important to understand, not only for your Salesforce Admin Certification, but also to build scalable solutions in the future. Happy solutionizing!
Hi Christine,
Using the example above, if a user has read access to Candidate object but not to Job Position, if we create the Junction Object, will the user be able to have read access to the Job Position? Sort of security inheritance for objects that the user have no access to. Thanks.
Best Regards,
klyap
When you grant access to the junction object (e.g. via the profile), when you select it, it will automatically select the two parent objects too and won't allow you to save unless the profile has read access to all 3 objects.
Hi Ken,
For users to be able to see Application records, they would need to be granted a minimum of Read access via their profile. When you select Read access against Applications, it will automatically select Read access on Job Position and Candidate. If you remove Read access from Job Position or Candidate, it will automatically remove Read access from Applications too!
Hope this helps!
If we build connection between two parent objects with Lookup relationship or one lookup and another master-detail relationship, wouldn't it still be called junction object??
Salesforce defines a Junction Object as:
"A custom object with two master-detail relationships. Using a custom junction object, you can model a “many-to-many” relationship between two objects."
https://help.salesforce.com/s/articleView?id=sf.relationships_manytomany.htm&type=5
Hello Everyone
I would like to know if in the example, is possible to create a record of Job position is possible to create a Candidate using all the relationships or the Candidate must be previously created?
Hi Sagar,
the connection should be common to make an object as a junction object. the junction object functionality should connect both master objects. if u make look-up., it won't connect both. For ex: Candidate and job position ., both are connected through Application., that's why it became a junction. so M-D relation with Candidate and Job Position is must., Look-up can't make them hold always. It's like by-passing the Application Object. Hope this Helps.
What if these need to be deleted after a time period to comply with data retention policy? In what order should the records be deleted in the flow? Or is there an option delete all at once?
Hi, am having one doubt, for example, we are using 3 objects job__c, candidate__c, Job application__c right. for suppose I need to display the list of candidates who applied for a particular job using Data-Table and screen flow likewise I need to display the list of jobs applied by candidates in job detail page. can anyone give me an idea to display the list of candidates and jobs using the junction object?
How many junction objects can the same set of 2 parent objects have together? Is there a limitation on this the same way there is a limitation on the max number of master-detail relationships an object can have?
What I find cumbersome about junction objects is the name field. Using autonumber provides a cryptic name that is meaningless to users. If you custom code your UI that's no big deal but SF is supposed to be "Click Not Code." If you use a text(80) like above then the user is forced to make up a name "Bob Applied for Dog Catcher" with every insert.
I often think that Salesforce should allow a calculated Name field. It would be create if it could calculate the name from the two other object names. But, that would get all of three votes on the idea exchange so it's not even worth entering.
Comments: