Writing Update Request Using Web API

In our earlier articles, 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.

To update data, we have the following two http methods:

PATCH

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’s say we want to update account record:

  1. var account = {};   
  2. account["address1_city"]="Delhi";    
  3. account["address1_country"]="India";   
  4. account["address1_street1"]="ABC Tower";   

After that we can pass it to record URI like the following,

  1. [Organization URL]+"/api/data/v8.0/accounts(Record GUID)"  

After that we can pass it to record URI like the following,

  1. function updateAccount(Id) {  
  2.     var serverURL = Xrm.Page.context.getClientUrl();  
  3.   
  4.     var account = {};  
  5.     account["address1_city"] = "Delhi";  
  6.     account["address1_country"] = "India";  
  7.     account["address1_street1"] = "ABC Tower";  
  8.   
  9.     var req = new XMLHttpRequest();  
  10.     req.open("PATCH", serverURL + "/api/data/v8.0/accounts(" + Id + ")"true);  
  11.     req.setRequestHeader("Accept""application/json");  
  12.     req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  13.     req.setRequestHeader("OData-MaxVersion""4.0");  
  14.     req.setRequestHeader("OData-Version""4.0");  
  15.     req.onreadystatechange = function() {  
  16.         if (this.readyState == 4 /* complete */ ) {  
  17.             req.onreadystatechange = null;  
  18.             if (this.status == 204)//OK {  
  19.                 alert("Account is updated")  
  20.             } else {  
  21.                 var error = JSON.parse(this.response).error;  
  22.                 alert(error.message);  
  23.             }  
  24.         }  
  25.     };  
  26.     req.send(JSON.stringify(account));  
  27. }  

PUT

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 the following,

  1. [Organization URL]+"/api/data/v8.0/accounts(Record GUID) /propertyname"  
  2.   
  3. Var account={"value":"value of the property"};  

So the complete code will be like below:

  1. function updateSingleProperty(Id) {  
  2.   
  3.     var serverURL = Xrm.Page.context.getClientUrl();  
  4.   
  5.     var account = {  
  6.         "value""1234"  
  7.     };  
  8.   
  9.     var req = new XMLHttpRequest();  
  10.     req.open("PUT", serverURL + "/api/data/v8.0/accounts(" + Id + ")/accountnumber"true); //we want to update accountnumber field  
  11.     req.setRequestHeader("Accept""application/json");  
  12.     req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  13.     req.setRequestHeader("OData-MaxVersion""4.0");  
  14.     req.setRequestHeader("OData-Version""4.0");  
  15.     req.onreadystatechange = function() {  
  16.         if (this.readyState == 4 /* complete */ ) {  
  17.             req.onreadystatechange = null;  
  18.             if (this.status == 204) {  
  19.                 alert("Account is updated")  
  20.             } else {  
  21.                 var error = JSON.parse(this.response).error;  
  22.                 alert(error.message);  
  23.             }  
  24.         }  
  25.     };  
  26.     req.send(JSON.stringify(account));}  

And in case if we need to update single-valued navigation (lookup), we can do it like following:

  1. var account = { "[email protected]":"/contacts(51366C53-45AE-E511-80DF-3863BB341BF0)"};  

And need to pass URI without property name like the following:

  1. serverURL+"/api/data/v8.0/accounts(Record GUID)"  

Stay tuned for more Web API Samples!


Similar Articles
HIMBAP
We are expert in Microsoft Power Platform.