Run a Workflow from a Button in the Ribbon

The other day a colleague asked me if I could help because it was necessary to run a CRM worflow from a button. So after a little bit of search on the web, with some test of some scripts, I’ve got the solution that worked for our case.

You will only need to set the guid of the Workflow that you want to run (workflowGuid) and entityId on which the Workflow was created and make the javascript to run from the button or event on the form.

Hope this helps!

function TriggerWorkflow(entityId, workflowGuid) {
var url = Xrm.Page.context.getServerUrl();
var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
url = url + OrgServicePath;
var request;

request = "xmlns:s=\'\'>" +
"<s:Body>" +
"xmlns=\'\' xmlns:i=\'\'>" +
"i:type=\'b:ExecuteWorkflowRequest\' xmlns:a=\'\' xmlns:b=\'\'>" +
"xmlns:c=\"<a href="">\</a>">" +
"<a:KeyValuePairOfstringanyType>" +
"<c:key>EntityId</c:key>" +
"i:type=\'d:guid\' xmlns:d=\'\'>" + <strong>entityId</strong> + "</c:value>" +
"</a:KeyValuePairOfstringanyType>" +
"<a:KeyValuePairOfstringanyType>" +
"<c:key>WorkflowId</c:key>" +
"i:type=\'d:guid\' xmlns:d=\'\'>" + <strong>workflowGuid</strong> + "</c:value>" +
"</a:KeyValuePairOfstringanyType>" +
"</a:Parameters>" +
"<a:RequestId i:nil=\'true\' />" +
"<a:RequestName>ExecuteWorkflow</a:RequestName>" +
"</request>" +
"</Execute>" +
"</s:Body>" +

var req = new XMLHttpRequest();"POST", url, false)

// Responses will return XML. It isn't possible to return JSON.
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "<a href=""></a>");

req.onreadystatechange = function () { assignResponse(req); }; //Alert to validate if the workflow ran. Method bellow.


function assignResponse(req) {
if (req.readyState == 4)
  if (req.status == 200)
    alert('successfully executed the workflow');

Leave a Reply

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

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