Monday, February 21, 2011

calling ondemand workflow using java script

Here is the logic to call on-demand workflow from java script.

function TriggerWorkflow() {

//  pass the workflow guid
var WorkflowId ="61A3C919-9636-44DC-87E4-C5FF2A63F2B4";

  var xml = "" +
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:Envelope xmlns:soap=\"\" xmlns:xsi=\"\" xmlns:xsd=\"\">" +
    GenerateAuthenticationHeader() +
    "<soap:Body>" +
    "<Execute xmlns=\"\">" +
    "<Request xsi:type=\"ExecuteWorkflowRequest\">" +
    "<EntityId>" + crmForm.ObjectId + "</EntityId>" +
    "<WorkflowId>" + WorkflowId + "</WorkflowId>" + //WorkflowId = guid of the workflow
    "</Request>" +
    "</Execute>" +
    "</soap:Body>" +

  var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
  xmlHttpRequest.setRequestHeader("SOAPAction", "");
  xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

  var resultXml = xmlHttpRequest.responseXML;
  return (resultXml.xml);


  1. I added my workflow GUID but nothing is happening, how do I debug? I'm trying to call this from CRM on office 365!

    1. What kind of error are you getting? Try debugging javascript. I tried long back, i'm not sure still this script working or not.