How to create Many to Many Relationship in Salesforce?

You can use master-detail relationships to model many-to-many relationships between any two objects. A many-to-many relationship allows each record of one object to be linked to multiple records from another object and vice versa.

Junction Object:
     A custom object with two master-detail relationships. Using a custom junction object, you can model a “many-to-many” relationship between two objects.

Creating the many-to-many relationship consists of:
  1. Creating the junction object.
  2. Creating the two master-detail relationships.
  3. Modify the related lists.
1. Creating the Junction Object
To create the junction object:
  • Click Your Name --> Setup --> Create --> Objects.
  • Click New Custom Object.
2. Creating the Two Master-Detail Relationships
To create the two master-detail relationships:
  1. Verify that the two objects you want to relate to each other already exist.
  2. On the junction object, create the first master-detail relationship field. In the custom field wizard:
  • Choose Master-Detail Relationship as the field type.
  • Select one of the objects to relate to your junction objec.
The first master-detail relationship you create on your junction object becomes the primary relationship. This affects the following for the junction object records:
            Look and feel: The junction object's detail and edit pages use the color and any associated icon of the primary master object.
            Record ownership: The junction object records inherit the value of the Owner field from their associated primary master record. Because objects on the detail side of a relationship do not have a visible Owner field, this is only relevant if you later delete both master-detail relationships on your junction object.
            Division: If your organization uses divisions to segment data, the junction object records inherit their division from their associated primary master record. Similar to the record ownership, this is only relevant if you later delete both master-detail relationships.

           Select a Sharing Setting option. For master-detail relationship fields, the Sharing Setting attribute determines the sharing access that users must have to a master record in order to create, edit, or delete its associated detail records.
           For the Related List Label that will display on the page layout of the master object, do not accept the default. Change this to use the name of the other master object in your many-to-many relationship.

           3. On the junction object, create the second master-detail relationship. In the custom field wizard:
  • Choose Master-Detail Relationship as the field type.
  • Select the other desired master object to relate to your junction object.
The second master-detail relationship you create on your junction object becomes the secondary relationship. If you delete the primary master-detail relationship or convert it to a lookup relationship, the secondary master object becomes primary.
           Select a Sharing Setting option. For master-detail relationship fields, the Sharing Setting attribute determines the sharing access that users must have to a master record in order to create, edit, or delete its associated detail records.
          For the Related List Label that will display on the page layout of the master object, do not accept the default. Change this to use the name of the other master object in your many-to-many relationship. 
  
3. Modify the related lists.


Sharing Setting:


Related list name:


Output:



1 comment:

  1. Awesome explanation -- I finally get it.

    ReplyDelete