Pages

Wednesday, 3 February 2016

Many to Many relation / junction object


Create a Many-to-Many Relationship

Available in: both Salesforce Classic and Lightning Experience
Available in: Contact Manager, Group, Professional, Enterprise, Performance, Unlimited, Developer, and Database.com Editions
Reports are not available in Database.com.


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 may have 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.
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. For example, you may have 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.
Creating the many-to-many relationship consists of:
  1. Creating the junction object.
  2. Creating the two master-detail relationships.
  3. Customizing the related lists on the page layouts of the two master objects.
  4. Customizing reports to maximize the effectiveness of the many-to-many relationship.

Creating the Junction Object

  1. Create a custom object to be your junction object.
  2. 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, it is recommended that you use the auto-number data type.
    • Do not launch the custom tab wizard before clicking Save. Junction objects do not need a tab.

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. For example, you may want to relate the standard case object to a custom bug object.
  2. On the junction object, create the first master-detail relationship field. In the custom field wizard:
    1. Choose Master-Detail Relationship as the field type.
    2. 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 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.
    3. 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.
    4. 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. For example, change this to Bugs so users will see a Bugs related list on the case detail page.
  3. On the junction object, create the second master-detail relationship. In the custom field wizard:
    1. Choose Master-Detail Relationship as the field type.
    2. 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.
    3. 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.
    4. 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. For example, change this to Cases so users will see a Cases related list on the bug detail page.

Customizing 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 might 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:
  1. 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.
  2. 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.
  3. 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 popup window. In the related list itself, only fields from the junction object are prefixed with the object name; fields from the other master object are not.
Note :
The junction object related list does not include an icon on the master record's detail pages because the junction object does not have a custom tab. If you make a tab for the junction object, the icon is included.

Customizing 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.

No comments:

Post a Comment