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=\'http://schemas.xmlsoap.org/soap/envelope/\'>" +
"<s:Body>" +
"xmlns=\'http://schemas.microsoft.com/xrm/2011/Contracts/Services\' xmlns:i=\'http://www.w3.org/2001/XMLSchema-instance\'>" +
"i:type=\'b:ExecuteWorkflowRequest\' xmlns:a=\'http://schemas.microsoft.com/xrm/2011/Contracts\' xmlns:b=\'http://schemas.microsoft.com/crm/2011/Contracts\'>" +
"xmlns:c=\"<a href="http://schemas.datacontract.org/2004/07/System.Collections.Generic/">http://schemas.datacontract.org/2004/07/System.Collections.Generic\</a>">" +
"<a:KeyValuePairOfstringanyType>" +
"<c:key>EntityId</c:key>" +
"i:type=\'d:guid\' xmlns:d=\'http://schemas.microsoft.com/2003/10/Serialization/\'>" + <strong>entityId</strong> + "</c:value>" +
"</a:KeyValuePairOfstringanyType>" +
"<a:KeyValuePairOfstringanyType>" +
"<c:key>WorkflowId</c:key>" +
"i:type=\'d:guid\' xmlns:d=\'http://schemas.microsoft.com/2003/10/Serialization/\'>" + <strong>workflowGuid</strong> + "</c:value>" +
"</a:KeyValuePairOfstringanyType>" +
"</a:Parameters>" +
"<a:RequestId i:nil=\'true\' />" +
"<a:RequestName>ExecuteWorkflow</a:RequestName>" +
"</request>" +
"</Execute>" +
"</s:Body>" +
"</s:Envelope>";

var req = new XMLHttpRequest();
req.open("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="http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute">http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute</a>");

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

req.send(request);
}

function assignResponse(req) {
if (req.readyState == 4)
{
  if (req.status == 200)
  {
    alert('successfully executed the workflow');
  }
  else
  {
     alert(req.status);
  }
}
}
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