Web API–Status update and SetState message Plugin


 

With Web API, now we can call the Update method to change the Statecode and Statuscode on the entity. This was not possible earlier with REST OrganizationData.svc endpoints.

On one hand it does makes it simpler to change the Status of the entity along with other attributes, one thing developer should keep in mind that. Now with Status Change, CRM will invoke Update message of and not SetState or SetStateDynamicEntity. It would be safer to register all 3 messages, that is Update, SetState and SetStateDynamicEntity so that all possible scenarios are covered.

Following is sample WebAPI code which update Account Status, this code will invoke Update message in Plugin.

    var entityId = Xrm.Page.data.entity.getId();
    var clientURL = Xrm.Page.context.getClientUrl();
   
    var AccountName = Xrm.Page.getAttribute(“name”).getValue();
 
    entityId = entityId.replace(“{“,””);
    entityId = entityId.replace(“}”, “”);
 
    var req = new XMLHttpRequest();
    var odataQuery = encodeURI(clientURL + “/api/data/v8.0/accounts(” + entityId + “)”);
    req.open(“PATCH”, odataQuery, false);
    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”);
 
    var account = {};
     // State code value
    account[“statecode”] = 1;
    //  status reason Value
    account[“statuscode”] = 2;
    // Normal field Update   
    account[“name”] =  AccountName + ” Status Changed”;
 
    req.send(JSON.stringify(account));
 
    if (req.readyState == 4) {
 
        if (req.status == 204) {
          alert(“Completed with Success”);
        } else {
            var error = JSON.parse(req.response).error;
            alert(error.message);
        }
    }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s