4 Feb 2016

Activate and Deactivate records using Javascript

Hello everyone,
Setting statecode and statuscode attributes using javascript in CRM is not easy as setting other fields unfortunately. But it can be done :)
Normally we expect below javascript code snippets to work but it does not if you try to change the statecode value.
 Xrm.Page.getAttribute("statecode").setValue(1);  


In this blog post I am going to talk about activate deactivate record using javascript in crm. You will learn how to change statecodes and statuscodes using javascript in CRM as well.
In this example I’m going to deactivate a custom entity record with a statuscode 2 which refers to Resolved in my case.
 function SetStateRequest(entityId, entityName, stateCode, statusCode) {  
   var SERVERURL = Xrm.Page.context.getServerUrl();  
   var OrgServicePath = "/XRMServices/2011/Organization.svc/web";  
   var requestMain = ""  
   requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";  
   requestMain += " <s:Body>";  
   requestMain += "  <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";  
   requestMain += "   <request i:type=\"b:SetStateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";  
   requestMain += "    <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";  
   requestMain += "     <a:KeyValuePairOfstringanyType>";  
   requestMain += "      <c:key>EntityMoniker</c:key>";  
   requestMain += "      <c:value i:type=\"a:EntityReference\">";  
   requestMain += "       <a:Id>" + entityId + "</a:Id>";  
   requestMain += "       <a:LogicalName>" + entityName + "</a:LogicalName>"; /////////////entity name///////////////////  
   requestMain += "       <a:Name i:nil=\"true\" />";  
   requestMain += "      </c:value>";  
   requestMain += "     </a:KeyValuePairOfstringanyType>";  
   requestMain += "     <a:KeyValuePairOfstringanyType>";  
   requestMain += "      <c:key>State</c:key>";  
   requestMain += "      <c:value i:type=\"a:OptionSetValue\">";  
   requestMain += "       <a:Value>" + stateCode + "</a:Value>";  
   requestMain += "      </c:value>";  
   requestMain += "     </a:KeyValuePairOfstringanyType>";  
   requestMain += "     <a:KeyValuePairOfstringanyType>";  
   requestMain += "      <c:key>Status</c:key>";  
   requestMain += "      <c:value i:type=\"a:OptionSetValue\">";  
   requestMain += "       <a:Value>" + statusCode + "</a:Value>";  
   requestMain += "      </c:value>";  
   requestMain += "     </a:KeyValuePairOfstringanyType>";  
   requestMain += "    </a:Parameters>";  
   requestMain += "    <a:RequestId i:nil=\"true\" />";  
   requestMain += "    <a:RequestName>SetState</a:RequestName>";  
   requestMain += "   </request>";  
   requestMain += "  </Execute>";  
   requestMain += " </s:Body>";  
   requestMain += "</s:Envelope>";  
   var req = new XMLHttpRequest();  
   req.open("POST", SERVERURL + OrgServicePath, false);  
   // Responses will return XML. It isn't possible to return JSON.  
   req.setRequestHeader("Accept", "application/xml, text/xml, */*");  
   req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");  
   req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");  
   var successCallback = null;  
   var errorCallback = null;  
   req.onreadystatechange = function () { SetStateResponse(req, successCallback, errorCallback); };  
   req.send(requestMain);  
   Xrm.Page.data.refresh();  
 }  
 //*********************************************************************************************  
 //Function: Set State Response  
 //*********************************************************************************************  
 function SetStateResponse(req, successCallback, errorCallback) {  
   if (req.readyState == 4) {  
     if (req.status == 200) {  
       if (successCallback != null)  
       { successCallback(); }  
     }  
     else {  
       errorCallback(_getError(req.responseXML));  
     }  
   }  
 }  
 //*********************************************************************************************  
 //Function: Get Error Xml  
 //*********************************************************************************************  
 function _getError(faultXml) {  
   var errorMessage = "Unknown Error (Unable to parse the fault)";  
   if (typeof faultXml == "object") {  
     try {  
       var bodyNode = faultXml.firstChild.firstChild;  
       //Retrieve the fault node  
       for (var i = 0; i < bodyNode.childNodes.length; i++) {  
         var node = bodyNode.childNodes[i];  
         //NOTE: This comparison does not handle the case where the XML namespace changes  
         if ("s:Fault" == node.nodeName) {  
           for (var j = 0; j < node.childNodes.length; j++) {  
             var faultStringNode = node.childNodes[j];  
             if ("faultstring" == faultStringNode.nodeName) {  
               errorMessage = faultStringNode.text;  
               break;  
             }  
           }  
           break;  
         }  
       }  
     }  
     catch (e) { };  
   }  
   return new Error(errorMessage);  
 }  

You can call above function to deactivate record like this,


 SetStateRequest(_entityId, logicalName, 1, 2);  

I used this function to set a custom entity’s statecode value. You can also set other entities’ statecodes and statuscodes. If so, you may want to refer to this post.

No comments:

Post a Comment