{"id":2043,"date":"2015-12-31T08:43:00","date_gmt":"2015-12-31T08:43:00","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=2043"},"modified":"2015-12-31T08:58:32","modified_gmt":"2015-12-31T08:58:32","slug":"writing-update-request-using-web-api","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=2043","title":{"rendered":"Writing update request using Web API"},"content":{"rendered":"<p>In our earlier posts, we discussed how to write create,retrieve and retrievemultiple requests, today we are going to discuss about writing update request using Web API for Dynamics CRM 2016.<br \/>\nYou can refer our below earlier posts:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/?p=2035\">Create Request<\/a><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/?p=1997\">Retrieve Request<\/a><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/?p=2012\">Retrievemultiple Request<\/a><\/p>\n<p>To update data , we have two below http methods are available:<\/p>\n<p><strong>PATCH <\/strong>&#8211; This method is used to update multiple property of particular entity type. To update multiple properties, we can create object and define properties that we want to update like below, let&#8217;s say we want to update account record:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar account = {};\r\naccount[&quot;address1_city&quot;]=&quot;Delhi&quot;;\r\naccount[&quot;address1_country&quot;]=&quot;India&quot;;\r\naccount[&quot;address1_street1&quot;]=&quot;ABC Tower&quot;; \r\n\r\n<\/pre>\n<p>After that we can pass it to record URI like below<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n[Organization URL]+&quot;\/api\/data\/v8.0\/accounts(Record GUID)&quot;\r\n<\/pre>\n<p>So complete code of using PATCH will be like below:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction updateAccount(Id) {\r\n    var serverURL = Xrm.Page.context.getClientUrl();\r\n\r\n    var account = {};\r\n    account[&quot;address1_city&quot;] = &quot;Delhi&quot;;\r\n    account[&quot;address1_country&quot;] = &quot;India&quot;;\r\n    account[&quot;address1_street1&quot;] = &quot;ABC Tower&quot;;\r\n\r\n    var req = new XMLHttpRequest();\r\n    req.open(&quot;PATCH&quot;, serverURL + &quot;\/api\/data\/v8.0\/accounts(&quot; + Id + &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)\/\/OK {\r\n                alert(&quot;Account is updated&quot;)\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><strong>PUT<\/strong>&#8211; This method is used to update single property of particular entity type. We need to pass the property name with entity URI itself and in object we can pass value of the property like below:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n[Organization URL]+&quot;\/api\/data\/v8.0\/accounts(Record GUID) \/propertyname&quot;\r\n\r\nVar account={&quot;value&quot;:&quot;value of the property&quot;};\r\n<\/pre>\n<p>So the complete code will be like below:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction updateSingleProperty(Id) {\r\n\r\n    var serverURL = Xrm.Page.context.getClientUrl();\r\n\r\n    var account = {\r\n        &quot;value&quot;: &quot;1234&quot;\r\n    };\r\n\r\n    var req = new XMLHttpRequest();\r\n    req.open(&quot;PUT&quot;, serverURL + &quot;\/api\/data\/v8.0\/accounts(&quot; + Id + &quot;)\/accountnumber&quot;, true); \/\/we want to update accountnumber field\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(&quot;Account is updated&quot;)\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<\/pre>\n<p>And incase if we need to update single-valued navigation (lookup), we can do it like below:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar account = { &quot;primarycontactid@odata.bind&quot;:&quot;\/contacts(51366C53-45AE-E511-80DF-3863BB341BF0)&quot;};\r\n<\/pre>\n<p>And need to pass URI without property name like below:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nserverURL+&quot;\/api\/data\/v8.0\/accounts(Record GUID)&quot;\r\n<\/pre>\n<p>Stay tuned for more Web API Samples !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our earlier posts, we discussed how to write create,retrieve and retrievemultiple requests, today we are going to discuss about writing update request using Web API for Dynamics CRM 2016. You can refer our below earlier posts: Create Request Retrieve Request Retrievemultiple Request To update data , we have two below http methods are available: PATCH &#8211; This method is&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=2043\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[275,296],"tags":[310,308,309,306,307],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2043"}],"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=2043"}],"version-history":[{"count":8,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2043\/revisions"}],"predecessor-version":[{"id":2051,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2043\/revisions\/2051"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}