Dynamics CRM 2011 · Javascript · Workflow

CRM 2011: Running/Executing Workflow using Javascript

This post explains how to run/execute CRM workflow using Javascript.

To run workflow, we need to send Execute Request.

var entityId = ""; //Guid of record that workflow is to run on.
var workflowId = ""; //Workflow Guid.

/*Generate Soap Body.*/
var soapBody = "<soap:Body>" +
		 "  <Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
		 "    <Request xsi:type=\'ExecuteWorkflowRequest\'>" +
		 "      <EntityId>" + entityId + "</EntityId>" +
		 "      <WorkflowId>" + workflowId + "</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 serverUrl = Xrm.Page.context.getServerUrl() + "/MSCRMservices/2007/crmservice.asmx";
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.open("POST", serverUrl, 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);

However I find that using workflow id might be problematic across deployment, so I wrote a function to retrieve workflow’s guid given the workflow’s name. However, please take some care in assigning workflow name to avoid pain.

function getWorkflowId(workflowName) {
    var odataSelect = Xrm.Page.context.getServerUrl() + '/XRMServices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=StateCode/Value eq 1 and ParentWorkflowId/Id eq null and Name eq \'' + workflowName + '\'';

    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", odataSelect, false);
    xmlHttp.send();

    if (xmlHttp.status == 200) {
        var result = xmlHttp.responseText;

        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(result);
        
        return xmlDoc.getElementsByTagName("d:WorkflowId")[0].childNodes[0].nodeValue;
    }
}

Cheers – Sy

Advertisements

2 thoughts on “CRM 2011: Running/Executing Workflow using Javascript

  1. You should note that ActiveXObject() is a part of IE and work only in IE. If one needs to take for other browsers he should use:
    10 …
    11 parser = new DOMParser();
    12 xmlDoc = parser.parseFromString(txt, “text/xml”);
    13 …

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s