CRM 2011 Call Workflow Using Javascript


*Example of script


function TriggerWorkflow(entityName,workflowGuid) {
  var xx = prompt(Xrm.Page.data.entity.getId(),Xrm.Page.data.entity.getId());

  /*Generate Soap Body.*/
  var soapBody = "<soap:Body>" +
                 "  <Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
                 "    <Request xsi:type=\'ExecuteWorkflowRequest\'>" +
                 "      <EntityId>" + Xrm.Page.data.entity.getId() + "</EntityId>" +
                 "      <WorkflowId>" + workflowGuid + "</WorkflowId>" +
                 "    </Request>" +
                 "  </Execute>" +
                 "</soap:Body>";

  /*Wrap the Soap Body in a soap:Envelope.*/
  var soapXml = "<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'>" +
                GenerateAuthenticationHeader() +
                soapBody +
                "</soap:Envelope>";

  /* Create the XMLHTTP object for the execute method.*/
  var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  xmlhttp.open("POST", "/MSCRMservices/2007/crmservice.asmx", false);
  xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");

  /* Send the XMLHTTP object. */
  xmlhttp.send(soapXml);
}

Purpose:
- Im creating a new button to trigger the workflow.
- Avoiding few clicking when running on demand workflow.
- Fast and easy to call.


0 comments: