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 10, 2017, 9:04 PM EDT
Thanks for this Article. Used a lot.
Joe WilsonPosted Jan 5, 2016, 11:58 PM EST
Thank you for sharing.
Ankit BansalPosted Jan 5, 2016, 9:15 PM EST
Thanks for sharing
Gowtham RajamanickamTOP 100Posted Jan 5, 2016, 8:20 PM EST
good article..
Humayun Kabir MamunPosted Jan 5, 2016, 6:05 PM EST
Nice...
Santhakumar MunuswamyPosted Jan 5, 2016, 6:01 PM EST
Thanks for nice article
Raja TPosted Jan 5, 2016, 5:43 PM EST
Nice, Thanks for sharing
Kumaresh RajalingamPosted Jan 5, 2016, 3:54 PM EST
Good one sir
Karthikeyan KPosted Jan 5, 2016, 9:08 AM EST
Good one sir...