{"id":2063,"date":"2016-01-05T16:41:31","date_gmt":"2016-01-05T16:41:31","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=2063"},"modified":"2016-03-17T06:43:01","modified_gmt":"2016-03-17T06:43:01","slug":"writing-associate-disassociate-request-using-web-api","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=2063","title":{"rendered":"Writing associate &#038; disassociate request using Web API"},"content":{"rendered":"<p>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 <a href=\"https:\/\/himbap.com\/blog\/?p=2043\">earlier sample <\/a>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.<\/p>\n<p>Let\u2019s 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.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/association3.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2066 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/association3-300x56.png\" alt=\"association3\" width=\"300\" height=\"56\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/association3-300x56.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/association3.png 433w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>So if we need to associate account record with building entity record, we can write <strong>POST<\/strong> request using following syntax can pass target record id as object:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n[OrganizationURL]+&quot;\/api\/data\/v8.0\/accounts(0370F447-C9B2-E511-80DF-3863BB35DED8)\/him_building_account\/$ref&quot;\r\n<\/pre>\n<p>Where<strong> him_building_account<\/strong> is the name of the relationship, we can check it by opening N:N relationship record:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/associate1.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2064 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/associate1-300x207.png\" alt=\"associate1\" width=\"300\" height=\"207\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/associate1-300x207.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/associate1.png 490w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nWe can use following code in our java script web resource for associate request:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction associateRequest(accountID, buildingID) {\r\n    var associate = {\r\n        &quot;@odata.id&quot;: serverURL + &quot;\/api\/data\/v8.0\/him_buildings(&quot; + buildingID + &quot;)&quot;\r\n    };\r\n    var serverURL = Xrm.Page.context.getClientUrl();\r\n    var req = new XMLHttpRequest();\r\n    req.open(&quot;POST&quot;, serverURL + &quot;\/api\/data\/v8.0\/accounts(&quot; + accountID + &quot;)\/him_building_account\/$ref&quot;, true);\r\n    req.setRequestHeader(&quot;Accept&quot;, &quot;application\/json&quot;);\r\n    req.setRequestHeader(&quot;Content-Type&quot;, &quot;application\/json; charset=utf-8&quot;);\r\n    req.setRequestHeader(&quot;OData-MaxVersion&quot;, &quot;4.0&quot;);\r\n    req.setRequestHeader(&quot;OData-Version&quot;, &quot;4.0&quot;);\r\n    req.onreadystatechange = function() {\r\n        if (this.readyState == 4 \/* complete *\/ ) {\r\n            req.onreadystatechange = null;\r\n            if (this.status == 204) {\r\n                alert('Record Associated');\r\n            } else {\r\n                var error = JSON.parse(this.response).error;\r\n                alert(error.message);\r\n            }\r\n        }\r\n    };\r\n    req.send(associate);\r\n}\r\n<\/pre>\n<p>And to disassociate records we can write following request using <strong>DELETE<\/strong> method:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction disassociateRequest(accountID, buildingID) {\r\n\r\n    var serverURL = Xrm.Page.context.getClientUrl();\r\n    var req = new XMLHttpRequest();\r\n    req.open(&quot;DELETE&quot;,serverURL+&quot;\/api\/data\/v8.0\/accounts(&quot;+accountID+&quot;)\/him_building_account\/$ref?$id=&quot;+serverURL+&quot;\/api\/data\/v8.0\/him_buildings(&quot;+buildingID+&quot;)&quot;, true);\r\n    req.setRequestHeader(&quot;Accept&quot;, &quot;application\/json&quot;);\r\n    req.setRequestHeader(&quot;Content-Type&quot;, &quot;application\/json; charset=utf-8&quot;);\r\n    req.setRequestHeader(&quot;OData-MaxVersion&quot;, &quot;4.0&quot;);\r\n    req.setRequestHeader(&quot;OData-Version&quot;, &quot;4.0&quot;);\r\n    req.onreadystatechange = function() {\r\n        if (this.readyState == 4 \/* complete *\/ ) {\r\n            req.onreadystatechange = null;\r\n            if (this.status == 204) {\r\n                alert('Record Disassociated');\r\n            } else {\r\n                var error = JSON.parse(this.response).error;\r\n                alert(error.message);\r\n            }\r\n        }\r\n    };\r\n    req.send();\r\n}\r\n<\/pre>\n<p><strong>Note<\/strong>: You can get complete system and custom entity reference for Web API using<br \/>\n[Organizaiton URL] + &#8220;\/api\/data\/v8.0\/<\/p>\n<p>Stay tuned for more Web API Samples !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=2063\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":2066,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[275,296],"tags":[319,315,316,317,318,314],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2063"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2063"}],"version-history":[{"count":10,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2063\/revisions"}],"predecessor-version":[{"id":2075,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2063\/revisions\/2075"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/2066"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}