2 Ways to Create a Junction Object in Salesforce

A Junction Object in Salesforce is a special kind of object that facilitates the many-to-many relationships between two objects.

In this Salesforce tutorial, I will explain junction objects in Salesforce with examples. I will also explain two different ways to create a junction object in Salesforce Lightning and Salesforce Classic.

What is Junction Object in Salesforce

The junction object in Salesforce is defined as a custom object with two master-detail fields.

In Salesforce, to create many-to-many relationships between objects in Salesforce, you need to create a custom object that is linked with two other objects of Salesforce via two master-detail relationships.

Junction Object Example in Salesforce

Let’s suppose you are creating an application for “University Management” in Salesforce. You came up with the case where you have two objects, “Students” and “Courses”. and here, each student enrolls in multiple courses, and each course also has multiple students.

It is not possible to show this with a simple object as it is not a simple parent-child relationship. It is a many-to-many relationship., so to deal with this kind of scenario, the need for a “Junction Object” is created.

You simply need to create a junction object named “Enrollment” and link the other two objects with it using the master-detail relationship field.

Salesforce Junction Object Limitations

The following are the limitations of junction objects in Salesforce:

  • You cannot upsert data into the junction object.
  • To create a junction object, two need exactly two master-detail relationships; you can do it on lookup relationship, but it destroys the true meaning of the junction object.
  • If you try to delete any one of the master records, the child record will also be deleted.
  • The child record cannot be deleted if the master record has more than 200 child records.
  • The Junction object cannot be the master to any of the other objects.

Which type of field do administrators use when creating a Junction Object?

To create a junction object in Salesforce, administrators need two Master-Detail Relationship fields to create a connection between two objects with a junction object.

The first master detail created on the junction object is called the Primary Master, and the second master detail is created on the junction object called the Secondary Master.

Whoever is the owner of the Primary Master will be the owner of the corresponding Junction (child records).

How to Create a Junction Object in Salesforce

You can create junction objects in Salesforce Lightning and Salesforce Classic. There are two different ways to create a junction object in Salesforce: create object and schema builder.

See also  How to create Approval Process in Salesforce

Create a Junction Object in Salesforce Lightning using Create Object

Below are the steps to create a junction object in Salesforce Lightning using the create object manually process.

1. Create the custom object; go to the “Setup” option and click on the “Object Manager” on the navigation bar.

how to create junction object in salesforce

2. Click on “Create” and then click on the “Custom Object”.

how to create a junction object in salesforce

3. After clicking the custom object in Salesforce, there will be a “Label” option: enter USA student. Now there is a “Plural Label” option; enter USA students.

After that, you can see that the “Object Name” and “Record Name” are automatically filled.

create junction object salesforce

4. Scroll to the bottom of the page and select the checkbox “Launch New Custom Tab Wizard”.

Then click on the “Save” option.

create a junction object in salesforce

5. There is a “Tab Style” field; click on the field and select a style you like.

create junction object in salesforce

6. Click “Next”, then again “Next”, and click on “Save”.

salesforce create junction object

7. Now, create one more object called the USA group. This object is created the same as the USA student object named“USA Group”.

Now, you need to create a relationship. To create it, create another object, which is a Junction Object named as “USA Student in Group”.

junction object in salesforce

8. Enter the “Record Name” and set up “Format”. The datatype is in the form of “Auto Number”. Fill in the “Display Format” and “Starting Number”, and click on “Save”.

junction object salesforce

9. Create a field and relationship for the junction object. Click on the “Field and Relationships” option that is present on the left sidebar.

Choose “Master-Detail Relationship” as the data type and click on “Next”.

salesforce junction object

10. Select the other object to which this object is related. For example, “USA Student”. Then, click on the “Next” button.

Then, enter the “Label” and “Name” for the lookup field. For example, “USA Student”. After that, click on “Next”.

what is junction object in salesforce

11. Click “Next”, then again “Next”, and then “Add the custom related list”, and after that, click the “Save & Next” option.

what is a junction object in salesforce

12. Again, create the “Master-Detail Relationship” field type. Choose the “Related Object” as a “USA group” for the new relationship. Click on the “Next”.

Then, enter the “Label” and “Name” for the lookup field. For example, “USA group”. Click on “Next” -> “Next” -> “Save & Next”.

which type of field do administrators use when creating a junction object?

Now, this “USA Student in Group” object works as a junction object as you have two objects, “USA Student” and “USA Group,” to it with the help of a master-detail relationship.

In this way, you can create junction objects in Salesforce Lightning.

Create Junction Object Salesforce Classic using Create Object

Below are the steps to create a junction object using create object in Salesforce Classic:

See also  How to Create Field Update Using Workflow rule in Salesforce

Create First Custom Object

1. Click on the “Setup” option. Scroll down and search for the “Build” section, click on the “Create” and select the “Objects” option.

junction object example in salesforce

2. To start the process of creating a new custom object, click on the “New Custom Object” button.

what is junction object in salesforce with example

3. Enter the “Label” as “Project USA” and “Plural Label” as “Projects USA”. The “Object Name” and “Record Name” are automatically filled, then click on the “Save & New” button to save it and start creating others.

junction objects salesforce

Create Second Custom Object:

4. Now, create the secondary master object. Enter the “Label” as “Developer USA” and “Plural Label” as “Developers USA”.

The “Object Name” and “Record Name” are automatically filled, then click on “Save & New”.

salesforce junction object limitations

Create Junction Object:

5. Now create the junction object; enter the “Label” as “Assignment USA” and “Plural Label” as “Assignments USA”.

You can see that the “Object Name” and “Record Name” are automatically filled, then click on “Save”.

junction object

Create Tab Style For Objects:

6. Scroll to the bottom of the page. You can see that on the left side, there is a “Build” option; click on the “Create” drop-down sign. Click on the “Tab” option. The custom tab Page is open; click on the “New” option.

create a custom junction object for job postings

7. Select the “Object”. Then select “Tab Style”, and after that, click on the “Next” option.

junction object in salesforce example

8. After choosing the tab, click “Next”, then again “Next”, and then “Save”.

master detail relationship salesforce

Note: Repeat Steps 6 to Step 8 for the other two objects also.

Create Master-Detail Relationship with Junction Object

9. Choose the object that you want to create as a “Junction Object”. Go to “Custom Fields & Relationships” and click on the “New” button.

what is a junction object in salesforce?

10. Select the “Master-detail relationship” as the data type and click on “Next”.

junction relationship in salesforce

11. Now select the other object to which the object is related and click on the “Next” option.

what is junction object

12. Enter the “Field Label” and “Field Name” for the lookup field; after entering both, click on “Next”.

master-detail relationship salesforce

13. Click “Next” -> “Next” -> “Add the custom related list” -> Click the “Save & Next” option.

example of junction object in salesforce

Note: Repeat Step 9 to Step 13 and create a master-detail relationship with another object that you want to link with the junction object. In my case, it is “Developer USA”.

In this way, you can create a junction object in Salesforce Classic using the create object option.

Create Junction Object Salesforce Lightning using Schema Builder

Below are the steps to create a junction object in Salesforce Lightning using schema builder.

1. From the “Setup,” search for the “Schema Builder” in the “Quick Find” box; once found, click on it.

junction object example

2. Create a new “Object” by simply clicking on the “Element” tab that is present in the left sidebar. Then, “Drag” the object onto the canvas.

junction object in salesforce with example

3. Enter the details for the new custom object that you are creating, such as “Label” as “USA Project” and “Plural Label” as “USA Projects”.

See also  How to Pass a Record ID into Salesforce Flow

You can see that the “Object Name” and “Record Name” are automatically filled, then click on “Save”.

creating a junction object salesforce

4. Follow the same steps to create two more objects named “USA Employee” and “USA Task Project”.

how to create junction object in salesforce

5. Drag-drop the “Master-detail” field onto the “USA Task Project” object, fill in all the details and click on the “Save” button.

Create a Master-Detail Relationship for both the objects “USA Employee” and “USA Project”.

how to create a junction object in salesforce

You can see that the junction object is successfully created as you have established a master-detail relationship of two objects on one object named USA Task Project.

create junction object salesforce

In this way, you can create a junction object in Salesforce Lightning using schema builder.

Create Junction Object in Salesforce Classic using Schema Builder

The following are the steps to create a junction object using the schema builder in Salesforce Classic.

1. Click on “Setup”, search for “Schema Builder” in the “Quick Find” box, and once you find it, click on it.

create a junction object in salesforce

2. To create a custom object, click on the “Element” tab given on the left side, and search for the “Object” and once found, drag it to the canvas.

create junction object in salesforce

3. Fill the necessary information to create a custom object. For example, enter the “Label” as “Applicant” and “Plural Label” as “Applicants”.

The “Object Name” and the “Record Name” fields get populated automatically based on the label entered. Click on the “Deployed” radio button in the development section and “Save” it.

salesforce create junction object

4. Repeat the step 3 and create two more custom objects named as “Job Position” and “Job Application”.

junction object in salesforce

5. Decide which object you want to treat as a junction object in Salesforce. For example, you want to make “Job Application” as a junction object.

Now, to convert it into a junction object, drag-drop the “Master-Detail” field onto it to create a many-to-many relationship between the custom object “Applicant”.

junction object salesforce

6. Again, drag-drop the “Master-Detail” onto the Job Application and create a relationship between the “Job Application” and the “Job Position”. Fill in all the details and click on the “Save” button.

salesforce junction object

You can see that the relationship between the objects is created through one custom object using master detail to make that common object act as the junction object.

what is junction object in salesforce

In this way, using the schema builder, you can create junction objects in Salesforce Classic.

Conclusion

I hope you have understood the junction object in Salesforce. I have explained two different ways to create junction objects in Salesforce step-by-step using the create object option and the schema builder option.

Additionally, I have guided you on how to create junction objects in both the user interfaces Salesforce Lightning and Salesforce Classic with an example.

You may like to read: