Execute Workflow using JScript in CRM 2011

Posted: May 30, 2012 in CRM 2011, Jscripts

To Execute a Workflow using jscript, first we need to get the workflow id by passing the process name.

In my example i’m performing an ODATA call to get the Workflow id, so don’t forget to add jquery1.4.1.min and json2 files in your Form Properties. You can get those files from sdk.

path – \sdk\samplecode\js\restendpoint\jqueryrestdataoperations\jqueryrestdataoperations\scripts.

Finally here is your jscript code to trigger the workflow.

 // function to trigger workflow
function TriggerWorkflow() {
    // get the workflow id
    var workflowId = GetProcessId();
    if (workflowId[0].results.length > 0) {
        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\">" +
       GenerateAuthenticationHeader() +
       "<soap:Body>" +
       "<Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
       "<Request xsi:type=\"ExecuteWorkflowRequest\">" +
       "<EntityId>" + Xrm.Page.data.entity.getId() + "</EntityId>" +
       "<WorkflowId>" + workflowId[0].results[0].WorkflowId + "</WorkflowId>" +
       "</Request>" +
       "</Execute>" +
       "  </soap:Body>" +
       "</soap:Envelope>" +
       "";
        var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", true);
        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);
    }
}

// function to get workflow id by performing and ODATA call.
function GetProcessId() {
    // pass the workflow name manually to get the Id of the workflow
    var processName = "Send Email on Contact Create";

    // get the server url
    var serverUrl = Xrm.Page.context.getServerUrl();
    var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=Name eq '" + processName + "' and ActiveWorkflowId/Id ne null";
    var jSonArray = new Array();

    jQuery.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: oDataUri,
        async: false,
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.             
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (data && data.d != null) {
                jSonArray.push(data.d);
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            alert("Error :  has occured during retrieval of the workflowId");
        }
    });
    return jSonArray;
}
Advertisements

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