I have read about many-to-many relationship in Salesforce and it is possible through Junction Object. That I got, but what I haven't got is that "How it works internally ?" Please help me to visualize the concept of Junction Object.

  • Accept the answer which provided the required information you needed so that other people with same question get benefit from it. – Vigneshwaran G Jul 20 '15 at 16:01

To create Many-to-Many relationship you need Junction object(as you mentioned in your question).

Lets take Opportunity and Product for example to explain this.

If you want Opportunity A should have Product A and Product B, and Product A should be available in Opportunity A and Opportunity B you should have a Junction object to hold the relationship.

So OpportunityLineItem is the Junction object which will hold multiple records in combination of Opportunity and Product like below

OppotunityLineItem1 - Opportunity A, Product A

OppotunityLineItem2 - Opportunity A, Product B

OppotunityLineItem2 - Opportunity B, Product A

Opportunity A have Product A and Product B and Product A is available in Opportunity A and Opportunity B.

Hope it helps.

  • So you are telling like if Junction Object will contain only those fields which are in relationship with both objects. Like result of Cross Product of two tables. – SFDC Mafia - VIC Jul 17 '15 at 9:14
  • It will have master detail relation to both objects. It can additionally have fields specific to it. – Vigneshwaran G Jul 17 '15 at 9:16
  • 1
    Still i'm not clear :( – SFDC Mafia - VIC Jul 17 '15 at 9:27
  • 1
    This is A good Example...you should let it sink in your mind..inside the Junction Object you have 2 Lookups (Can be as MasterDetail). One is pointing at Product and the Other is Pointing to Opportunity. This Relation between the 2 Objects has Attributes Fields Such as Quantity Sold and Date for example are relevant only to the Opportunity Line Items... Then you can aggregate the results of total Quantity to Opportunity Level.. – sfdx bomb Jul 17 '15 at 9:51
  • @YuvalV / Vigneshwaran : Can you tell me what is OppotunityLineItem1 here? Is it field/column or record/data? Actually i'm trying to understand how the relations of two objects are stored in Junction Object. – SFDC Mafia - VIC Jul 17 '15 at 11:09

It may be worth creating a Developer Org if you haven't already and checking out the Force.com Platform Fundamentals guide which will take you through creating a recruitment application for a pseudo-company and covers this type of question. It's also a great introduction into earning the DEV-401 qualification!

Let's go with this example. Picture having your Candidates object for potential employees and your Positions object for jobs. You can't relate these directly because one candidate could apply to many positions and positions should have many candidates. This is where a junction object comes in. In this example, you'd have another object called Applications. A candidate can create many applications and many applications can be linked to a position.

There are two fields to achieve this: the Lookup and Master-Detail fields. Check out this YouTube video for a more in-depth explanation, but effectively:

Master Detail:

  • You cannot have a detail record without a master.
  • The detail record inherits sharing rules from the master.
  • You cannot update the relationship to the master in a master-detail relationship.
  • The number of master-detail relationships you can use are limited.
  • You cannot set profile object permissions for a detail record.
  • Master-detail relationships are automatically included in report record types.


  • Lookups are generally for use where you may or may not need to have a relationship between two objects.
  • Lookups are generally used to reference commonly shared data, such as reference data.
  • Lookups are used to link two objects together when you don't want the behaviour of the master-detail - particularly around sharing rules, profile permissions and cascade delete.
  • Lookups are used when you need to relate multiple 'parents' to the detail record.

Hope this explains things for you!



Ultimately, the best way to understand how these relationships work, is to try to create one in your Sandbox. This Salesforce Documentation is the only guide you need to follow.

But this may be easier to understand if you consider how these relationships appear from a user's point of view.
The user doesn't need to know that the Junction records exist. The only difference is that rather than a lookup field appearing in the detail section of a page layout, the other object's record is looked up in the related records section, after clicking the New Junction Object Name button on the related record list.

The below show's a Junction object, linking Projects to Opportunities.

ProjectxOpp Lookup

Once the relationship's been created, you can choose to display the fields of the record that's been looked up, in either object's related records list, rather than the fields from the Junction object itself.

Many To Many Related List Customisation

So for all intents & purposes, the looked up record appears to be the record that's related to the record which is being viewed (a Project, in this example).
The end result is that the related record's name is displayed as one of the related record's fields, which can be clicked to access that record directly.

Related Records List


Salesforce supports 2 kinds of relationships like Master Detail and Lookup. They are both one-to-many relationship, and they are both defined from the many-to-one side, that is from a child to a parent. They can be made one-to-one relationship by adding validation rules, or maybe triggers to enforce the one-to-one nature, i.e. only one child is allowed.

Junction objects are used to create many to many relationships between objects. If you take the Recruiting application example, you can see that a Position can be linked to many Candidates, and a Candidate can apply for different Positions. To create this data model you need a third object "Job Application" that links the 2.

So you'd create a lookup field for both Position and Candidate object on the "Job Application" object. This will establish many to many relationship between Position and Candidate via the "Job Application" object known as the junction object.

Fore more information, read this article https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.