Admins

What is a Junction Object in Salesforce?

By Ben McCarthy

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!

READ MORE: The 6 Types of Relationships in Salesforce

The Author

Ben McCarthy

Ben is the Founder of Salesforce Ben. He also works as a Non-Exec Director & Advisor for various companies within the Salesforce Ecosystem.

Comments:

    Eduard
    July 06, 2021 12:57 pm
    Hi Ben, can we build Junction Objects with Lookup relationship instead of Master-Detail?
    Mark
    October 27, 2021 11:30 pm
    no mention of security?
    Christine Marshall
    October 28, 2021 9:28 am
    Hi Mark, Was there anything in particular you felt would have been useful? When you say "security" do you mean the sharing model etc? Best, Christine
    klyap
    November 23, 2021 9:42 am
    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
    Christine Marshall
    November 23, 2021 10:42 am
    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.
    Wayne
    December 14, 2021 6:49 pm
    Could you clarify if the junction object is the master or the detail?
    Christine Marshall
    December 15, 2021 1:58 pm
    The junction object is the child object (detail) in a master detail relationship.
    Wayne
    December 17, 2021 8:24 pm
    Thanks Christine
    Ken
    December 21, 2021 12:46 am
    So if users don't have read access to the Job Position object, they can still view the Job Application records?
    Josh
    December 23, 2021 8:40 am
    I'm curious about this as well - what are the implications of using Lookup relationships instead of M-D.
    Christine Marshall
    January 11, 2022 4:44 pm
    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!
    Sagar
    January 25, 2022 6:37 am
    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??
    Christine Marshall
    January 25, 2022 9:28 am
    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
    Diego Nieto
    April 13, 2022 5:13 am
    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?
    Ran
    May 25, 2022 7:31 am
    You can definitely build one. But Master-detail relationships are better suited for junction objects to prevent orphan records.
    N S Rao
    July 11, 2022 8:16 am
    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.
    Anya
    September 21, 2022 10:38 am
    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?
    VINA FASALE
    September 27, 2022 3:23 pm
    Can we create many to many relationships without using junction object in lookup
    Christine Marshall
    September 27, 2022 4:10 pm
    No - a Junction object is what enables you to create a many to many relationship.
    Tilottama
    October 13, 2022 8:19 am
    Hi Chistine, Can we delete junction object?
    Christine Marshall
    October 13, 2022 11:46 am
    Yes, you can delete junction objects if required.
    Ramesh
    February 28, 2023 4:14 pm
    Which access Junction object have when its 1st master object have Read/write access and its 2nd object have read access?
    saicharan M H
    March 01, 2023 7:50 am
    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?
    Carlos
    April 07, 2023 3:22 am
    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?
    Babitha
    May 02, 2023 12:53 pm
    Hi Can we create Junction Object, if child object contains previous data .

Leave a Reply