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
To create Many-to-Many relationship you need Junction object(as you mentioned in your question).
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.
OpportunityLineItem is the Junction object which will hold multiple records in combination of
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.
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:
- 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.
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.
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.
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