Change Record Status using Javascript in CRM

Hi,

Sometimes we get the requirement to change the status of the record on the button click or some where using JavaScript.

Here is the JavaScript of it.

// Method Calling
vId = Xrm.Page.data.entity.getId();
vEntityName = Xrm.Page.data.entity.getEntityName();
changeRecordStatus(vId, vEntityName, 1, 2);

function changeRecordStatus(RECORD_ID, Entity_Name, stateCode, statusCode ) {

    var url = “”;
    if(Xrm.Page.context.getClientUrl) {
        //Post UR 12
        url = Xrm.Page.context.getClientUrl();
    }
    else {
        //Pre UR 12
        url = Xrm.Page.context.getServerUrl();
    }
    // create the SetState request
    var request = ;
    request += ;
    request += “”;
    request += “”;
    request += ;
    request +=
;
    request += “EntityMoniker”;
    request += “”;
    request +=
+ RECORD_ID + ;
    request += +Entity_Name+;
    request += ;
    request += “”;
    request +=
;
    request += ;
    request += “State”;
    request += “”;
    request +=
+ stateCode + ;
    request += “”;
    request += ““;
    request += ;
    request += “Status”;
    request += “”;
    request +=
+ statusCode + ;
    request += “”;
    request += ““;
    request += ““;
    request += ;
    request +=
SetState;
    request += “”;
    request += “”;
    request +=
;
    request +=
;
    //send set state request
    $.ajax({
        type: “POST”,
        contentType: “text/xml; charset=utf-8”,
        datatype: “xml”,
        url: url + “/XRMServices/2011/Organization.svc/web”,
        data: request,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader(“Accept”, “application/xml, text/xml, */*”);
            XMLHttpRequest.setRequestHeader(“SOAPAction”, http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute”);
        },
        success: function (data, textStatus, XmlHttpRequest) {
            Xrm.Page.data.refresh();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
   });
}
Hope this helps.


Happy CRM’ing
Gopinath.

3 thoughts on “Change Record Status using Javascript in CRM

  1. saly September 28, 2015 / 5:09 pm

    This comment has been removed by the author.

    Like

  2. Mike B December 28, 2016 / 4:43 pm

    Awesome, thanks for the help, this worked great!

    Like

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s