Create a Many-to-Many Object Relationship
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. For example, you create a custom object called Bug that relates to the standard case object such that a bug could be related to multiple cases and a case could also be related to multiple bugs. When modeling a many-to-many relationship, you use a junction object to connect the two objects you want to relate to each other.
Required Editions
| Available in: both Salesforce Classic and Lightning Experience |
Available in: Contact Manager, Group, Professional, Enterprise, Performance, Unlimited, Developer, and Database.com Editions Reports aren’t available in Database.com. |
| User Permissions Needed | |
|---|---|
| To create a many-to-many relationship: | Customize Application |
A custom object with two master-detail relationships. Using a custom junction object, you can model a “many-to-many” relationship between two objects.
For example, you create a custom object called “Bug” that relates to the standard case object such that a bug could be related to multiple cases and a case could also be related to multiple bugs.
How to create many-to-many relationships:
- Create the junction object.
- Create the two master-detail relationships.
- Customize the related lists on the page layouts of the two master objects.
- Customize reports to maximize the effectiveness of the many-to-many relationship.
- Create a custom object to be your junction object.
- In the custom object wizard, consider these tips specifically
for junction objects:
- Name the object with a label that indicates its purpose, such as BugCaseAssociation.
- For the Record Name field, we recommend that you use the auto-number data type.
- Don’t launch the custom tab wizard before clicking Save. Junction objects don’t need a tab.
Create the Two Master-Detail Relationships
To create the two master-detail relationships:
- Verify that the two objects you want to relate to each other exist. For example, you want to relate the standard case object to a custom bug object.
- 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 object. For example, select
Case.
The first master-detail relationship you create on your junction object becomes the primary relationship. This relationship 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 don’t have a visible Owner field, this inherited value is only relevant if you later delete both master-detail relationships on your junction object.
- Division: If your org uses divisions to segment data, the junction object records inherit their division from their associated primary master record. Similar to the record ownership, this inherited division 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 to create, edit, or delete its associated detail records.
- For the Related List Label that's displayed on the page layout of the master object, don’t accept the default value. Change the value to use the name of the other master object in your many-to-many relationship. For example, change the value to Bugs so users see a Bugs related list on the case detail page.
- 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. For example, select
Bug.
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 to create, edit, or delete its associated detail records.
- For the Related List Label that displays on the page layout of the master object, don’t accept the default value. Change this value to use the name of the other master object in your many-to-many relationship. For example, change this value to Cases so users see a Cases related list on the bug detail page.
Customize Many-to-Many Relationship Related Lists
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. For example, you can change the BugCaseAssociations related list to Cases on the bugs page layout and to Bugs on the cases page layout. You can further customize these related lists to display fields from the other master object.
To customize the fields that display in the junction object related list on each master object page layout:
- Edit the page layout of each master object that is related to the junction object. For example, modify the BugCaseAssociations related list for case records by editing the page layout for cases.
- Edit the properties of the related list you want to modify. For example, on cases the BugCaseAssociations related list was renamed to Bugs, so select the Bugs related list.
- Add the fields to display in the related list. You can add
fields from the junction object itself, but more
importantly, you can add fields from the other master
object.
Each field is prefixed with its object name in the window. In the related list itself, only fields from the junction object are prefixed with the object name; fields from the other master object aren’t.
Customize Reports for Many-to-Many Relationships
Many-to-many relationships provide two standard report types that join the master objects and the junction object. The report types are:
- “Primary master with junction object and secondary master” in the primary master object's report category.
- “Secondary master with junction object and primary master” in the secondary master object's report category.
The order of the master objects in the report type is important. The master object listed first determines the scope of records that can be displayed in the report.
You can create custom reports based on these standard report types. In addition, you can create custom report types to customize which related objects are joined in the report.

