Sunday, February 24, 2013

Change Record Status using javascript in CRM 2011

Sometimes we needs to change the CRM record status in java script. Following is the java script code to change the record status(deactivate,activate,qualify etc.). In this example i'm going to deactivate a custom entity record.

 
function changeRecordStatus(RECORD_ID,stateCode,statusCode) {

        // create the SetState request
        var request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        request += "<s:Body>";
        request += "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
        request += "<request i:type=\"b:SetStateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
        request += "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
        request += "<a:KeyValuePairOfstringanyType>";
        request += "<c:key>EntityMoniker</c:key>";
        request += "<c:value i:type=\"a:EntityReference\">";
        request += "<a:Id>" + RECORD_ID + "</a:Id>";
        request += "<a:LogicalName>cmic_systemusersalesterritoryassociation</a:LogicalName>";
        request += "<a:Name i:nil=\"true\" />";
        request += "</c:value>";
        request += "</a:KeyValuePairOfstringanyType>";
        request += "<a:KeyValuePairOfstringanyType>";
        request += "<c:key>State</c:key>";
        request += "<c:value i:type=\"a:OptionSetValue\">";
        request += "<a:Value>"+stateCode+"</a:Value>";
        request += "</c:value>";
        request += "</a:KeyValuePairOfstringanyType>";
        request += "<a:KeyValuePairOfstringanyType>";
        request += "<c:key>Status</c:key>";
        request += "<c:value i:type=\"a:OptionSetValue\">";
        request += "<a:Value>"+statusCode+"</a:Value>";
        request += "</c:value>";
        request += "</a:KeyValuePairOfstringanyType>";
        request += "</a:Parameters>";
        request += "<a:RequestId i:nil=\"true\" />";
        request += "<a:RequestName>SetState</a:RequestName>";
        request += "</request>";
        request += "</Execute>";
        request += "</s:Body>";
        request += "</s:Envelope>";

        //send set state request
        $.ajax({
            type: "POST",
            contentType: "text/xml; charset=utf-8",
            datatype: "xml",
            url: Xrm.Page.context.getServerUrl() + "/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.ui.close();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
   

}

//Deactivating custom entity record
changeRecordStatus(Xrm.Page.data.entity.getId(),1,2);


Hope it helps!!!


4 comments:

  1. Thanks for the code! There is a minor problem with the code, you are not using the value for state and status code:

    request += "" + stateCode + "";
    request += "";
    request += "";
    request += "";
    request += "Status";
    request += "";
    request += "" + statusCode + "";

    ReplyDelete
    Replies
    1. Rayan, Thanks for correcting. it's a typo.

      Delete
    2. Hi Guru Prasad Could Please send me ASAP a complete code for Activate and Deactivate of Custom Entity Record and I would like to do this functionality on OnSave Event.
      Thanks In Advance.
      siva

      Delete
  2. could you please modify the original code accordingly and republish - i'm not understand Guru's changes and how they link into the original code.

    ReplyDelete