How to Create Junction Object in Salesforce

I worked as a Salesforce admin for an organization that manages multiple projects. The organization’s multiple teams often work on several projects at the same time. However, each project may involve many teams working on it. Recently, I was required to track the activity of teams that are currently working on a specific project.

To achieve this requirement in Salesforce, we have a junction object, using which we can create many-to-many relationships between two objects.

Here, we will learn about junction objects in Salesforce, and I will explain how to create junction objects in Salesforce with examples and step-by-step explanations.

Junction Object in Salesforce

In Salesforce, we can create one-to-one or one-to-many relationships using master-detail or lookup relationships. But when we want to create many-to-many relationships between two objects, we need to create a third object called a junction object. In that junction object, we add records of the other two objects by using a foreign key that references the primary key of the other two objects.

In a many-to-many relationship, the junction object we create is always on the child’s side. That means it will be the many-sided object, which will reference the parent records of the other two objects.

When we create a junction object, we need to create a master-detail relationship between it and other parent objects. This is because when we delete the parent object’s records, the records related to that object from the junction object should also be deleted. Always remember that the junction object always has two master-detail relationship fields.

The primary and secondary relationships are automatically created when we create the junction object. In the below, I explained these relationships:

Primary Relationship: When creating a master-detail relationship between a parent object and a junction object, the field you create first will become the primary relationship field, and with which object you create, that field will become the primary master object.

Secondary Relationship: After creating a relationship with the first object, the other relationship will become the secondary relationship field, and that object will become the secondary master object.

So, the junction object with which we have a primary relationship or which is the primary master object will have all the properties of that object, such as owner name and sharing setting, applied to the junction object.

Many-to-Many Relationship using Junction Object in Salesforce

Using the following diagram, we will try to understand how to create a many-to-many relationship between two objects in Salesforce.

There are two objects: Teams and Projects. We want to create a many-to-many relationship between these two objects to assign multiple teams to multiple projects. To achieve this in another database, we need to create a different table to show the many-to-many relationship between the two tables.

However, in Salesforce, we need to create a third object called a Junction object. Only using the junction object can we create many-to-many relationships in Salesforce.

Now, in the junction object, you can see the Account team and QA team working on multiple projects. Using the junction object, we can show many-to-many relationships between two objects.

What is Junction Object in Salesforce

Create Junction Object in Salesforce

For example, you have two custom object teams and projects. Each team can work on multiple projects, and each project can have multiple teams. The requirement is that you need to manage teams and projects and track which teams are working on which Projects.

Here, I created two objects, Teams and Projects, which will be the parent records of the junction object, which we will create in the following steps. In these objects, I created some teams and project records.

Junction Object in Salesforce

Then, we need to create another Custom Object on which we will create a master-detail relationship related to team and project objects. I created a custom object named Project Assignment with an auto-number field named Project Assignment ID.

When we create two master-detail relationships on the same object, it will become the Junction object in Salesforce.

Create Junction Object in Salesforce

1.  Go to the Setup option, select the Object Manager tab, and search for the object that will be on the many side and that you want to make a junction object. Then click the Fields & Relationships option. We need to create a master-detail relationship on this object as a new field. For that, we have the New button. Click on it.

How to Create Junction Object in Salesforce

2. In the Data Type list, you can see the field Master-Detail Relationship. Click on it, then click the Next button to set the next steps.

junction object salesforce

3. In the Related Object, you need to select any parent object on which we want to create the many-to-many relationship. First, I selected the Team object, which will become the primary master object, and the Project object, which will become the secondary master object. Then, click on the Next button.

junction object in salesforce

4. Here, you can set a Field Label, which will display on the record details page of the junction (Project Assignment) object. I have entered the Team Name as a label. The Field Name automatically populates. This will be the API Name. Then click on the Next button.

how to create a junction object in salesforce

5. In the Establish field-level security for reference field section, we give visibility to the profiles. As the MDR field is required, it is visible to all profiles by default. Here, we click on the Next button.

Then, you will see the Child Relationship Name, leave it as it is, and click the Next button again.

what is a junction object in salesforce

6. In the final step, we need to enter the Related List Label. This means that when we open any parent record, we want to see the records under that particular record that will be available in the Related List. Then, select the page layout on which we want to add this field.

After saving this, the master-detail relationship between the project-assigned object and the team object will be created. We want to create an MDR between the project and this same object. To do so, click the Save & New button.

what is junction object in salesforce

7. Follow the steps we used to create MDR on the team and project-assigned object. But here in the Related to field, we need to select the Project object, which is another parent object. It will be the secondary master object for the junction object.

salesforce junction object

Then, enter the field label and click the Next button.

create junction object salesforce

8. I entered Team Assign to Project in the Related List Label because this related list will be on the project object. When we open the related list tab from the project object, we can see the team assigned to a particular project. Now click the Save button.

create a junction object in salesforce

Now, here you can see we have created two master-detail relationships on the Project Assignment object, which is the junction object.

junction object Salesforce

9. Now, navigate to the junction object you created and click the New button to create records. Here, we have the Project Assignment ID field, which we created when creating the project assignment object. It is an auto-number field, so it will be read-only. After saving the record, you will see a value in that field.

Then, on this object, we created a master-detail field related to the team object. As you click on this field, you will see records that you created in the team object. Here, you need to select the team name.

Another master detail field is related to the project object. As you click on this field, you will see records that you created in the project object. Here, you need to select the project name.

That means you assign projects to a particular team, and you can assign multiple teams to several projects. If multiple teams work on several projects, it will be a may-to-may relationship. This many-to-many relationship can only be achieved using Junction Object.

many to many relationship salesforce

After creating records, when you open the project assignment list view here, you can see multiple teams working on multiple projects. For example, the Account Team and QA Team are working on Insight Hub and DevOps Nexus projects, too. That means many teams are working on many projects.

many to many relationship in salesforce

To differentiate it, go to the Team object and open any team record. Here, I selected the Account team record.

junction object example in salesforce

As you open the account team record, open the Related tab, and scroll down to see Project Assigned to Team. Here, you can see multiple projects assigned to this team, and other teams are also working on several projects in which this account team is involved.

junction object in salesforce example

Similarly, when you open any project record, I opened the Insight Hub project-related list, and here you can see multiple teams assigned to this project, and other projects can also be assigned to these teams.

salesforce many to many relationship

In this way, we can create many-to-may relationships in Salesforce using the junction object.

Conclusion

I hope you have an idea about many-to-many relationships in Salesforce. Now that we have seen why we need to create an M-M relationship in Salesforce, I have explained how to create a junction object to create a many-to-many relationship in Salesforce with an example. Using the junction object, we can easily track the activity of teams that are currently working on a specific project.

You may like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.