Monday 4 January 2016

MS CRM 2016 Web API Operations - Update a single property value


Update a single property value :

When you want to update only a single property value use a PUT request with the property name appended to the Uri of the entity.

Whenever you want to update single attribute of an entity it can be done by using PUT verb.

Note : while creating object use "value"  property to accommodate the Updated value of attribute.


function UpdatSinglePropertyAccountByPUT() {

debugger;
var clientURL = Xrm.Page.context.getClientUrl();
var AccountId =  "B3A2B300-A8AF-E511-80DD-6C3BE5A878BC";

//Attribute which you want to update e.g Name
var Property ="name";

var AccountTobeUpdated ={};
AccountTobeUpdated["value"] ="My first Update On Account by API using PUT";

UpdatAccountByPUT(AccountId,clientURL,AccountTobeUpdated,Property)


}

 

function UpdatAccountByPUT(AccountId,clientURL,AccountTobeUpdated,Property)
{
 
var req = new XMLHttpRequest();
req.open('PUT', clientURL + "/api/data/v8.0/accounts(" + AccountId + ")/"+Property, true);
req.setRequestHeader("Content-type","application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");

req.onreadystatechange = function () {
 if (this.readyState == 4 /* complete */) {
  req.onreadystatechange = null;
  if (this.status == 204) {   
   console.log("Updated account with ID: "+ AccountId)
  }
  else {
   var error = JSON.parse(this.response).error;
   console.log(error.message);
  }
 }
};
req.send(JSON.stringify(AccountTobeUpdated));
 

}


No comments:

Post a Comment