{"id":3031,"date":"2018-06-27T00:25:43","date_gmt":"2018-06-27T00:25:43","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=3031"},"modified":"2018-06-30T22:26:07","modified_gmt":"2018-06-30T22:26:07","slug":"creating-connection-using-webapi-sample-code","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=3031","title":{"rendered":"Creating Connection using WebAPI- {Sample Code}"},"content":{"rendered":"<p>If you are looking for sample code to create connection record using WebAPI for Dynamics 365 CE, this post will help you. I am creating connection record for opportunity, you can change lookups entity based on your requirement.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction CreateConnection() {\r\n    \/\/get opportunity id\r\n    var opportunityid = Xrm.Page.data.entity.getId().substring(1, 37); \/\/remove {}\r\n\r\n    \/\/get owner of opportunity\r\n    var ownerid = Xrm.Page.getAttribute(&quot;ownerid&quot;).getValue()[0].id;\r\n    ownerid = ownerid.substring(1, 37); \/\/remove {}\r\n\r\n    \/\/get server url\r\n    var serverURL = Xrm.Page.context.getClientUrl();\r\n\r\n    \/\/prepare entity object\r\n    var connection = {};\r\n    \/\/set record1id lookup with opportunity\r\n    connection[&quot;record1id_opportunity@odata.bind&quot;] = &quot;\/opportunities(&quot; + opportunityid+ &quot;)&quot;;\r\n\r\n    \/\/set record2id lookup with opportunity owner\r\n    connection[&quot;record2id_systemuser@odata.bind&quot;] = &quot;\/systemusers(&quot; + ownerid + &quot;)&quot;;\r\n    \/\/setup connection role\r\n    connection[&quot;record2roleid@odata.bind&quot;] = &quot;\/connectionroles(EA5B38CE-A5EE-4CA0-A339-3A51B7DA87FE)&quot;; \/\/connection role\r\n\r\n    connection.effectiveend = new Date();\r\n\r\n    var req = new XMLHttpRequest();\r\n    req.open(&quot;POST&quot;, Xrm.Page.context.getClientUrl() + &quot;\/api\/data\/v8.2\/connections&quot;, true);\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.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.onreadystatechange = function () {\r\n        if (this.readyState === 4) {\r\n            req.onreadystatechange = null;\r\n            if (this.status === 204) {\r\n                Xrm.Utility.alertDialog('Connection Created...');\r\n            } else {\r\n                Xrm.Utility.alertDialog(&quot;Error while creating Connection&quot;+this.statusText);\r\n            }\r\n        }\r\n    };\r\n    req.send(JSON.stringify(connection));\r\n}\r\n<\/pre>\n<p>Hope it will help someone !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are looking for sample code to create connection record using WebAPI for Dynamics 365 CE, this post will help you. I am creating connection record for opportunity, you can change lookups entity based on your requirement. Hope it will help someone !!<\/p>\n","protected":false},"author":1,"featured_media":3036,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[402,522,275,492],"tags":[525,524,526,527,523],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3031"}],"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=3031"}],"version-history":[{"count":3,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3031\/revisions"}],"predecessor-version":[{"id":3045,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3031\/revisions\/3045"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/3036"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}