CRM 2011 + Execute workflow from Ribbon Button JavaScript


This post shows how to execute a workflow on click of a ribbon button. Make sure to make the Workflow an On Demand Workflow. Following screenshot shows how to do it:


ExecuteWorkflow = function(entityId, workflowId){

// Soap Request for Executing Workflow
                                        var executeWorkflowSoapRequest = "" +
                    "<?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>" + entityId + "</EntityId>" +
                    "        <WorkflowId>" + workflowId + "</WorkflowId>" +
                    "      </Request>" +
                    "    </Execute>" +
                    "  </soap:Body>" +
                    "</soap:Envelope>" +
                    "";

// using ajax for the request
                    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", executeWorkflowSoapRequest.length);
                    xmlHttpRequest.send(executeWorkflowSoapRequest);

// response of the workflow request
                    var resultXml = xmlHttpRequest.responseXML;
}

function OnClickRibbon()
{
// Get the guid of the workflow created. This can be retrieved from the url of the workflow
// page.
var workflowProcessID = '034EA44C-44D3-4470-B418-10265F5DA8E0';

// Get entity Name
 var entityName = crmForm.ObjectTypeName;

// Get Type Code for entity
var entityID = var etc = Xrm.Page.context.getQueryStringParameters().etc;
// stores the response whether workflow has executed or any error.
var executeWorkflowResponse = '';

executeWorkflowResponse = ExecuteWorkflow(entityID, workflowProcessID);
}

Advertisements

One thought on “CRM 2011 + Execute workflow from Ribbon Button JavaScript

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s