{"id":2035,"date":"2015-12-30T19:26:12","date_gmt":"2015-12-30T19:26:12","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=2035"},"modified":"2016-04-10T09:52:33","modified_gmt":"2016-04-10T09:52:33","slug":"writing-create-request-using-web-api","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=2035","title":{"rendered":"Writing create request using Web API"},"content":{"rendered":"<p>In our earlier posts we provided sample code to write <a href=\"https:\/\/himbap.com\/blog\/?p=1997\">retrieve <\/a>and <a href=\"https:\/\/himbap.com\/blog\/?p=2012\">retrievemultiple <\/a>request, today we are going to provide sample code to create entity record using Web API. Let say we want to create account record and want to set different fields of different types. There are mainly two things which is different in Web API while creating entity record, the first is referring lookup field and the other is capturing response.<\/p>\n<p>In Web API relationship is represented using Navigation properties, there are two types of navigations properties <strong>Single-valued<\/strong> and <strong>Collection-valued<\/strong>. Single-valued navigation is used to represent many to one (N:1) navigation whereas Collection-valued is used to represent one to many and many to many navigation property. You can find more details <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt607990.aspx#bkmk_navprops\">here<\/a><\/p>\n<p>To set the lookup (<strong>single-valued<\/strong>) navigation property we can use two options, like following:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/account[&quot;primarycontactid@odata.bind&quot;]=&quot;\/contacts(757B1E74-FBA3-E511-80DE-3863BB341BF0)&quot;;\r\n<\/pre>\n<p>In above code, we are setting primary contact id using existing contact record, but if required we can also create contact record on the fly and set lookup using following option:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\naccount[&quot;primarycontactid&quot;]={&quot;firstname&quot;:&quot;Mahender&quot;,&quot;lastname&quot;:&quot;Pal&quot;};\r\n<\/pre>\n<p>After create request is executed successfully, http response status code is returned as <strong>204<\/strong>, which does not include any contents so if you will try to debug code, you will see response as blank like below:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/WebAPICreate1.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2036 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/WebAPICreate1-300x275.png\" alt=\"WebAPICreate1\" width=\"300\" height=\"275\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/WebAPICreate1-300x275.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/WebAPICreate1.png 453w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nBut we can get GUID of the new record using following code<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var accountUri = this.getResponseHeader(&quot;OData-EntityId&quot;);<\/pre>\n<p>So let\u2019s create a java script web resource and use following code under text editor<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction createAccount() {\r\n\r\n    var serverURL = Xrm.Page.context.getClientUrl();\r\n    var account = {};\r\n    account[&quot;name&quot;] = &quot;Web API Example&quot;;\r\n    account[&quot;address1_city&quot;] = &quot;Delhi&quot;;\r\n\r\n    \/\/account[&quot;primarycontactid@odata.bind&quot;]=&quot;\/contacts(757B1E74-FBA3-E511-80DE-3863BB341BF0)&quot;;  \/\/setting existing lookup\r\n\r\n    account[&quot;primarycontactid&quot;] = {\r\n        &quot;firstname&quot;: &quot;Mahender&quot;,\r\n        &quot;lastname&quot;: &quot;Pal&quot;\r\n    };\r\n\r\n    \/\/optionset\r\n    account[&quot;industrycode&quot;] = 1;\r\n    \/\/two options\r\n    account[&quot;donotphone&quot;] = true;\r\n    \/\/number\r\n    account[&quot;numberofemployees&quot;] = 20;\r\n    \/\/date time\r\n    account[&quot;him_approvaldate&quot;]=new Date();\r\n\r\n    var req = new XMLHttpRequest();\r\n    req.open(&quot;POST&quot;, serverURL + &quot;\/api\/data\/v8.0\/accounts&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                var accountUri = this.getResponseHeader(&quot;OData-EntityId&quot;);\r\n                var ID = accountUri.substr(accountUri.length - 38).substring(1, 37); \/\/get only GUID\r\n                Xrm.Utility.openEntityForm(&quot;account&quot;, ID); \/\/Open newly created account record\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(JSON.stringify(account));\r\n}\r\n<\/pre>\n<p>After that we can call this method on some event or through command button to create account record.<\/p>\n<p>Stay tuned for more Web API Samples !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our earlier posts we provided sample code to write retrieve and retrievemultiple request, today we are going to provide sample code to create entity record using Web API. Let say we want to create account record and want to set different fields of different types. There are mainly two things which is different in Web API while creating entity&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=2035\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":2036,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[275,296],"tags":[304,305,301],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2035"}],"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=2035"}],"version-history":[{"count":8,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2035\/revisions"}],"predecessor-version":[{"id":2235,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2035\/revisions\/2235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/2036"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}