Dynamics 365 CE

Associate and Disassociate Records in Dynamics 365 using XRM WebAPI (JavaScript)

Associate and Disassociate Records in Dynamics 365 using XRM WebAPI (JavaScript)

Recently, I worked on a Dynamics 365 customization where I needed to associate selected subgrid records with a parent record - specifically, linking multiple contacts to an account using a custom button.

Initially, I thought of using Power Automate or a custom plugin with an action, but after exploring more, I discovered that the XRM WebAPI already provides built-in methods to associate and disassociate records. This approach turned out to be much simpler and faster.

Scenario Overview

I had a Contact subgrid on the Account form showing “All Active Contacts.”

The goal was simple:

  • Select a few contacts from the subgrid.
  • Click a custom button labeled “Associate.”
  • Automatically link those selected contacts to the current account record.

To make it more interactive, I also added another subgrid showing only the related contacts for that account.

When I clicked the “Associate” button, the selected contacts appeared instantly in that related subgrid.

Associate Records using XRM WebAPI

The Associate action works perfectly for relationships such as One-to-Many and Many-to-Many.
Here’s the full JavaScript code I used:

Demonstration

Associate and Disassociate Records in Dynamics 365 using XRM WebAPI (JavaScript)2. Added two Contact subgrids on the Account form:

-   One shows All Active Contacts.
-   Another shows Related Contacts. ![Associate and Disassociate Records in Dynamics 365 using XRM WebAPI (JavaScript)](/assets/images/051/img_1238f49940.png)4.  Created a custom subgrid button labeled "Associate". ![Associate and Disassociate Records in Dynamics 365 using XRM WebAPI (JavaScript)](/assets/images/051/img_49e580ab1a.png)6.  On clicking it, the selected contacts from the active view got linked with the account — instantly reflecting in the related subgrid.

Disassociate Records using XRM WebAPI

Just like the associate action, we can disassociate records easily with a similar approach.
Here’s the sample code:

Reference

Have a great day!

Tube Flash Kit Logo

XRM Flash Code

VS Code-style editor for Dynamics 365 CRM. Edit, format, and publish Web Resources natively (JS, HTML, CSS, XML).

Add to Chrome

Comments