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.
- [OrganizationURL]+"/api/data/v8.0/accounts("+accountID+")/him_building_account/$ref"
- function associateRequest(accountID, buildingID) {
- var associate = {
- "@odata.id": serverURL + "/api/data/v8.0/him_buildings(" + buildingID + ")"
- };
- var serverURL = Xrm.Page.context.getClientUrl();
- var req = new XMLHttpRequest();
- req.open("POST", serverURL + "/api/data/v8.0/accounts(" + accountID + ")/him_building_account/$ref", true);
- req.setRequestHeader("Accept", "application/json");
- req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
- req.setRequestHeader("OData-MaxVersion", "4.0");
- req.setRequestHeader("OData-Version", "4.0");
- req.onreadystatechange = function() {
- if (this.readyState == 4 /* complete */ ) {
- req.onreadystatechange = null;
- if (this.status == 204) {
- alert('Record Associated');
- } else {
- var error = JSON.parse(this.response).error;
- alert(error.message);
- }
- }
- };
- req.send(associate);
- }
- function disassociateRequest(accountID, buildingID) {
- var serverURL = Xrm.Page.context.getClientUrl();
- var req = new XMLHttpRequest();
- req.open("DELETE",serverURL+"/api/data/v8.0/accounts("+accountID+")/him_building_account/$ref?$id="+serverURL+"/api/data/v8.0/him_buildings("+buildingID+")", true);
- req.setRequestHeader("Accept", "application/json");
- req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
- req.setRequestHeader("OData-MaxVersion", "4.0");
- req.setRequestHeader("OData-Version", "4.0");
- req.onreadystatechange = function() {
- if (this.readyState == 4 /* complete */ ) {
- req.onreadystatechange = null;
- if (this.status == 204) {
- alert('Record Disassociated');
- } else {
- var error = JSON.parse(this.response).error;
- alert(error.message);
- }
- }
- };
- req.send();
- }
- [Organizaiton URL] + "/api/data/v8.0/

shivaram santoshPosted Oct 11, 2017, 1:04 AM
Thanks for this Article. Used a lot.
Joe WilsonPosted Jan 6, 2016, 4:58 AM
Thank you for sharing.
Ankit BansalPosted Jan 6, 2016, 2:15 AM
Thanks for sharing
Gowtham RajamanickamPosted Jan 6, 2016, 1:20 AM
good article..
Humayun Kabir MamunPosted Jan 5, 2016, 11:05 PM
Nice...
Santhakumar MunuswamyPosted Jan 5, 2016, 11:01 PM
Thanks for nice article
Raja TPosted Jan 5, 2016, 10:43 PM
Nice, Thanks for sharing
Kumaresh RajalingamPosted Jan 5, 2016, 8:54 PM
Good one sir
Karthikeyan KPosted Jan 5, 2016, 2:08 PM
Good one sir...