Many-to-Many Relationships

In addition to one-to-many relationships between objects, objects can also have a many-to-many relationship between each other. For example, a service request can have multiple employees working on it. At the same time, a single employee can work on multiple service requests.

In this scenario, you would create a many-to-many relationship between the Service Request and Resource objects, where the related records from both objects store their primary identifiers in an intersection object. Many-to-many relationships aren't supported in desktop work areas.

Once you create a many-to-many relationship, you can't delete it.

Creating Many-to-Many Relationships: Example

To create a many-to-many relationship using Application Composer:

  1. Select Relationships in the Common Setup pane.

  2. On the Relationships page, click the New icon.

  3. Select the source object and target object.

    A child object can't be the source object or target object.

    The Note common component isn't available for selection as either a source object or target object.

    Once you create a relationship, you can no longer edit the source and target objects.

    Note: You can create only one many-to-many relationship for a particular set of objects.
  4. Enter the relationship name and description.

    Once you create a relationship, you can no longer edit the relationship name.

  5. Indicate the cardinality of the relationship:

    • M:M

      • Many-to-many

  6. Enter the name of the intersection object.

    The intersection object's table records two foreign keys: one for the Service Request object and the other for the Resource object. This enables the many-to-many relationship.

    The intersection object is available as an extensible, top-level object in Application Composer. Optionally extend the intersection object. Custom fields that you add to the intersection object are available for display on the subtabs you create, which is discussed in the section below.

    Intersection objects also have a corresponding web service automatically published.

    You can optionally enable auditing for this intersection object so that changes to this object's attributes display in the Audit History work area. To set up audit for intersection objects, use the Manage Audit Policies task in the Setup and Maintenance work area and select CRM Application Composer as the product.

  7. Optionally specify data filter criteria for both the source and target objects.

    The filter criteria that you specify here controls which records are available for association at runtime with a record from the other object in this relationship.

    Read: "Configuring a Search and Select Dialog for Custom Objects".

Adding Subtabs

After you create the many-to-many relationship, you can now add related object subtabs on each object's application details page:

Note: You can add subtabs for a many-to-many relationship to application details pages only. Many-to-many relationships aren't supported in desktop work areas.
  • Create an Employee subtab on the service request's details page.

    The subtab displays all employees that are working on a specific service request. At runtime, your end users can add or remove employees who are working on a specific service request.

    When creating the subtab, you can select which Resource fields to display, such as Employee Name and Title. You can also select which intersection object fields to display, such as Primary Service Request Owner.

  • Create a Service Requests subtab on an employee's details page.

    The subtab displays all service requests that an employee is working on, since each employee can work on multiple service requests. At runtime, your end users can add or remove service requests that an employee is working on.

    When creating the subtab, you can select which Service Request fields to display, such as Service Request Abstract and Date Logged. You can also select which intersection object fields to display, such as Primary Service Request Owner.

When selecting the fields for display on a related object subtab, join fields aren't available for selection if the relationship is a many-to-many relationship.

Configuring a Search and Select Dialog for Custom Objects

A Search and Select dialog lets your end users search for and select object records when assigning one record to another, such as an employee to a service request. These dialog boxes are launched from the related object subtabs that you create, after creating relationships.

Search and Select dialog boxes are automatically provided for standard objects, and aren't extensible. However, if you're creating a many-to-many relationship that involves a custom object, then you must configure the Search and Select dialog boxes for those custom objects.

The filter criteria that you specify in the relationship definition applies to the Search and Select dialog, and controls which records are available for association at runtime with a record from the other object in this relationship.

For example, you can define filter criteria that lets your end users select only "unassigned" service requests for association with an employee.

Groovy Script Syntax

Once you have created a many-to-many relationship, two foreign key fields (one for each object) are created on the intersection object. Use the following API names to access those foreign keys in your scripts.

Relationship Type

Foreign Key API Name

Pattern Used

Reference relationship (many-to-many)

If the source object name is SourceObj_c, the target object name is TargetObj_c, and the intersection object is IntersectionObject_c, then the two foreign key API names (added to the intersection object) are TargetObj_Id_Tgt_TargetObj_cToIntersectionObject_c and SourceObj_Id_Src_SourceObj_cToIntersectionObject_c.

<Name of the object>_Id_Tgt_<Name of the object>_cTo<Intersection object name>, <Name of the object>_Id_Src_<Name of the object>_cTo<Intersection object name>