There are various relationships in which Salesforce by default allows only one-to-many and many-to-one relationships which can be achieved by using various triggers. But what if in an organization there is a manager who wants to access both sales and support team data?
The manager wants to edit, view, and update information in the database. To do this he/she needs access to both sales and support team databases which can be done if he/she is provided with many-to-many relationships or as we say master-detail relationships in Salesforce. This can be done by using Junction Object in Salesforce.
Learn Salesforce in detail by watching the video on Salesforce Training-
What is Junction Object in Salesforce?
Salesforce allows the use of Junction Objects which enables many-to-many relationships. Junction Object is a type of custom object which allows linking one custom object or one custom instance to many children records and each child can equally be linked to many parents.
Junction Object in simple terms can be called a custom object with two master details fields.
Now a common question arrives in all Salesforce interviews: what is the use of Junction Object? The answer is, junction object is used to link one exemplar of a particular record to many child records and many child records to one record respectively.
Applying for a Salesforce Admin job? Check out our blog on top Salesforce Admin Interview Questions and Answers!
Best practices for the creation of Junction Object in Salesforce-
- The first master-detail created on the junction object is known as the primary master
- The second master-detail created on the junction object is known as the secondary master.
- Whosoever is the owner of the primary master will be the owner of corresponding child records.
- If we delete any one of the master records the corresponding child records will get deleted too.
What are Many-to-Many relationships in Salesforce?
A many-to-many relationship allows each record of one object to be linked to multiple records from another object and vice versa. For example, a many-to-many relationship exists between customers and products: customers can purchase various products, and products can be purchased by many customers.
For a many-to-many relationship in Salesforce, each master object record displays a related list of the associated junction object records. To create a seamless user experience, you can change the name of the junction object-related list on each of the master object page layouts to have the name of the other master object.
learn Salesforce from the scratch with Salesforce Tutorial!
Steps to create a Junction Object in Salesforce
Let’s understand how to create a Junction Object in Salesforce by understanding it with an example- we will be creating three objects namely Project, Project Task, and Employee. Now in this situation master record will be created on Employee and thus, he will be able to access both the project and the project task records.
Step 1 – Open your Salesforce Dashboard.
Step 2 – On the Quick Search tab search Schema Builder. Click and open it. A page will appear
Step 3 – So at first, we are going to create our objects. Let’s create an object by dragging the object from the list shown on the left.
Check out the blog on Salesforce Trailhead.
Step 4 – Create a new object named Project -> Add Projects as plural labels. Click on Save after entering the necessary information.
Step 5 – Create another object named Employee -> Add Employees as plural labels. Click on Save after entering the necessary information.
Preparing for a Salesforce Interview! Check out our Salesforce Interview Questions and Answers.
Get 100% Hike!
Master Most in Demand Skills Now !
Step 6 – Create another object named Project Task -> Add Project Tasks as plural labels. Click on Save after entering the necessary information.
Step 7 – All your objects are created now and it will look like this:
Step 8 – Now drag Master Relationship from the list and create a Master Relationship role related to Project.
Step 9 – Now again drag Master Relationship from the list and create a Master Relationship role related to Employee.
Step 10 – Your Junction Object is created. Now you have access to many-to-many relationships in your Salesforce dashboard.
Also, learn what is custom settings from our blog on custom settings in Salesforce.
How to query Junction Object in Salesforce?
Creation of Junction Object is done by the following steps but it can also be accessed by the use of SOQL (Salesforce Object Query Language) query too. SOQL query can be used to access the data from the junction object. This can be done in the following way-
- Create a new relationship on your Document Package
- Change the “Type” to “SOQL”
- Give the relationship an alias. Be sure it is unique and does not match the naming convention of any existing objects or fields in your Salesforce Org.
- Paste the SOQL Query in the “SOQL” box
- For custom objects, be sure to append __r at the end for dot notated fields like this
SELECT lookup_(Object in Junction)api_name__r.field FROM junction_object_api_name WHERE main_object_api_name__r.id = '<<main_object_record_id_tag>>'
Interested in learning Salesforce? Enroll in Salesforce Training now!
Points to remember while creating a Junction Object in Salesforce-
- You need to customize an object so that it can be used as the junction object. Steps in customizing an object for this purpose differ from edition to edition.
- You need to first check if the objects to be related already exist.
- Check the page layouts of master objects for the lists.
- The objective for this is to maximize the effectiveness of the relationships.
- The ability to create many-to-many relationships is available only in the following editions: Salesforce Classic, Salesforce Lightning Experience, Group, Contact Manager, Enterprise, Professional, Developed, Unlimited, Performance, and the Database.com.
Have any questions about Junction Object? Get it cleared on the Salesforce community!