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 – 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:
var account = {}; account["address1_city"]="Delhi"; account["address1_country"]="India"; account["address1_street1"]="ABC Tower";
After that we can pass it to record URI like below
[Organization URL]+"/api/data/v8.0/accounts(Record GUID)"
So complete code of using PATCH will be like below:
function updateAccount(Id) { var serverURL = Xrm.Page.context.getClientUrl(); var account = {}; account["address1_city"] = "Delhi"; account["address1_country"] = "India"; account["address1_street1"] = "ABC Tower"; var req = new XMLHttpRequest(); req.open("PATCH", serverURL + "/api/data/v8.0/accounts(" + Id + ")", true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 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)//OK { alert("Account is updated") } else { var error = JSON.parse(this.response).error; alert(error.message); } } }; req.send(JSON.stringify(account)); }
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 below:
[Organization URL]+"/api/data/v8.0/accounts(Record GUID) /propertyname" Var account={"value":"value of the property"};
So the complete code will be like below:
function updateSingleProperty(Id) { var serverURL = Xrm.Page.context.getClientUrl(); var account = { "value": "1234" }; var req = new XMLHttpRequest(); req.open("PUT", serverURL + "/api/data/v8.0/accounts(" + Id + ")/accountnumber", true); //we want to update accountnumber field req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 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) { alert("Account is updated") } else { var error = JSON.parse(this.response).error; alert(error.message); } } }; req.send(JSON.stringify(account));}
And incase if we need to update single-valued navigation (lookup), we can do it like below:
var account = { "primarycontactid@odata.bind":"/contacts(51366C53-45AE-E511-80DF-3863BB341BF0)"};
And need to pass URI without property name like below:
serverURL+"/api/data/v8.0/accounts(Record GUID)"
Stay tuned for more Web API Samples !!
Pingback: Writing update request using Web API - Microsoft Dynamics CRM Community
1) How to update Look fields ?
Hi Chandini,
Thank you for your comment, you can update lookup field like below, this is also mentioned in last section of above post
var account = { “primarycontactid@odata.bind”:”/contacts(51366C53-45AE-E511-80DF-3863BB341BF0″};
Thank you
Pingback: Writing associate & disassociate request using Web API | HIMBAP