Monday, February 21, 2011

From Ribbon button calling On demand workflow for selected records of grid in CRM2011

From Ribbon button calling On demand workflow for selected records of grid in CRM2011


Add the following code in the customization.xml file while customizing the ribbon button
<CommandDefinitions>
          <CommandDefinition Id="Cmd_JavaScript2">
            <EnableRules>
              <EnableRule Id="Mscrm.Enabled" />
            </EnableRules>
            <DisplayRules />
            <Actions>
              <JavaScriptFunction Library="$webresource:new_callworkflow" FunctionName="myworkflow">
               // Add this line to get the list of selected records guid's
               <CrmParameter Value="SelectedControlSelectedItemReferences" />
              </JavaScriptFunction>
            </Actions>
          </CommandDefinition>
</CommandDefinitions>



Add this java script in the web resource "new_callworkflow"
function Myfunction(SelectedControlSelectedItemReferences) {

    var allItems = new Array
    var allItems = SelectedControlSelectedItemReferences

    var iTotal = allItems.length;
    if (iTotal > 0) {
        for (var i = 0; i < iTotal; i++) {
        //    alert(allItems[i].Id)
callworkfow(allItems[i].Id);
        }
        alert("Success");
    }
    else
      alert("Please select any record to perform required action");
  

}


function callworkfow(id)
{

try
{

var guid;

if(id == null)
{

 guid =Xrm.Page.data.entity.getId();
}
else
guid = id;

// assign workflow guid
var WorkflowId ="0ACBCECD-DE6C-4E86-A767-0B1EE34B03D2";

  var xml = "" +
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
    Xrm.Page.context.getAuthenticationHeader() +
    "<soap:Body>" +
    "<Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
    "<Request xsi:type=\"ExecuteWorkflowRequest\">" +
    "<EntityId>" + guid + "</EntityId>" +
    "<WorkflowId>" + WorkflowId + "</WorkflowId>" + //WorkflowId = guid of the workflow
    "</Request>" +
    "</Execute>" +
    "</soap:Body>" +
    "</soap:Envelope>";

  var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
  xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
  xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
  xmlHttpRequest.send(xml);

  var resultXml = xmlHttpRequest.responseXML;

if(id == null)
alert("Success");
  return (resultXml.xml);
}
catch(e)
{
  alert("Failed to Execute");
}
}

No comments:

Post a Comment