Writing Associate And Disassociate Request Using Web API

This article is about writing associate and disassociate request using Web API.

Associate and disassociate request is used to link and unlink records having relationship. We can link and unlink records depending on their relationship type, for example, we can link records having 1:N using update request (setting lookup). In our earlier sample, we demonstrated how we can update lookup field using Web API. In this post we are going to discuss associating and disassociating N:N relationship.

Let’s say we have a custom entity called Building and which is related to account using N:N relationship, so a single account can be related to multiple buildings, similarly single building can be related to multiple accounts.

 
  1. [OrganizationURL]+"/api/data/v8.0/accounts("+accountID+")/him_building_account/$ref"  
Where him_building_account is the name of the relationship, we can check it by opening N:N relationship record:

 
We can use the following code in our JavaScript web resource for associate request:
  1. function associateRequest(accountID, buildingID) {  
  2.     var associate = {  
  3.         "@odata.id": serverURL + "/api/data/v8.0/him_buildings(" + buildingID + ")"  
  4.     };  
  5.     var serverURL = Xrm.Page.context.getClientUrl();  
  6.     var req = new XMLHttpRequest();  
  7.     req.open("POST", serverURL + "/api/data/v8.0/accounts(" + accountID + ")/him_building_account/$ref"true);  
  8.     req.setRequestHeader("Accept""application/json");  
  9.     req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  10.     req.setRequestHeader("OData-MaxVersion""4.0");  
  11.     req.setRequestHeader("OData-Version""4.0");  
  12.     req.onreadystatechange = function() {  
  13.         if (this.readyState == 4 /* complete */ ) {  
  14.             req.onreadystatechange = null;  
  15.             if (this.status == 204) {  
  16.                 alert('Record Associated');  
  17.             } else {  
  18.                 var error = JSON.parse(this.response).error;  
  19.                 alert(error.message);  
  20.             }  
  21.         }  
  22.     };  
  23.     req.send(associate);  
  24. }  
And to disassociate records we can write the following request using DELETE method:
  1. function disassociateRequest(accountID, buildingID) {  
  2.   
  3.     var serverURL = Xrm.Page.context.getClientUrl();  
  4.     var req = new XMLHttpRequest();  
  5.     req.open("DELETE",serverURL+"/api/data/v8.0/accounts("+accountID+")/him_building_account/$ref?$id="+serverURL+"/api/data/v8.0/him_buildings("+buildingID+")"true);  
  6.     req.setRequestHeader("Accept""application/json");  
  7.     req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  8.     req.setRequestHeader("OData-MaxVersion""4.0");  
  9.     req.setRequestHeader("OData-Version""4.0");  
  10.     req.onreadystatechange = function() {  
  11.         if (this.readyState == 4 /* complete */ ) {  
  12.             req.onreadystatechange = null;  
  13.             if (this.status == 204) {  
  14.                 alert('Record Disassociated');  
  15.             } else {  
  16.                 var error = JSON.parse(this.response).error;  
  17.                 alert(error.message);  
  18.             }  
  19.         }  
  20.     };  
  21.     req.send();  
  22. }  
Note: You can get complete system and custom entity reference for Web API using:
  1. [Organizaiton URL] + "/api/data/v8.0/  
Stay tuned for more Web API Samples!