web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / HIMBAP / Writing update request usin...

Writing update request using Web API

Mahendar Pal Profile Picture Mahendar Pal 45,095

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 !!

HIMBAP | Need any help in Microsoft CRM Contact US

Comments

*This post is locked for comments