Updated on – 24th Sep 2015
Recently I had a requirement to trigger a workflow on click of ribbon button when it meets certain criteria. Refer below link for calling JavaScript function from custom ribbon button.
http://ankit.inkeysolutions.com/2012/01/crm-2011-how-to-use-visual-ribbon.html
The criteria values are checked using JavaScript and if all criteria are satisfied then the workflow is to be triggered. The function below will take two inputs where entityId is the id of the record for which the workflow will execute and workflowProcessId is the id of the workflow which we want to execute.
http://ankit.inkeysolutions.com/2012/01/crm-2011-how-to-use-visual-ribbon.html
The criteria values are checked using JavaScript and if all criteria are satisfied then the workflow is to be triggered. The function below will take two inputs where entityId is the id of the record for which the workflow will execute and workflowProcessId is the id of the workflow which we want to execute.
function startWorkflow(entityId, workflowProcessId) {
var request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<s:Body>" +
"<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
"<request i:type=\"b:ExecuteWorkflowRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">" +
"<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">" +
"<a:KeyValuePairOfstringanyType>" +
"<c:key>EntityId</c:key>" +
"<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + entityId + "</c:value>" +
"</a:KeyValuePairOfstringanyType>" +
"<a:KeyValuePairOfstringanyType>" +
"<c:key>WorkflowId</c:key>" +
"<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + workflowProcessId + "</c:value>" +
"</a:KeyValuePairOfstringanyType>" +
"</a:Parameters>" +
"<a:RequestId i:nil=\"true\" />" +
"<a:RequestName>ExecuteWorkflow</a:RequestName>" +
"</request>" +
"</Execute>" +
"</s:Body>" +
"</s:Envelope>";
var requestURL = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web";
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("POST", requestURL, false)
xmlHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
xmlHttpRequest.send(request);
}
That’s it. The above common method can be used to trigger workflow from JavaScript.
I hope this would be helpful.
Excellent
ReplyDeleteis it possible from a home page of an entity ? if i select 10 record in the same time ? does the workflow retrive the 10 GUID of the 10 records ?
ReplyDeleteNo, at present it runs only at record level(via entity form).
ReplyDeletewill not work on non IE Browsers. (Cross Browser functionality in UR 12) because of the ActiveX call.
ReplyDeleteYes, that's true, at present the above function did not support the other browsers.
ReplyDeleteHi ankit, how and where we can find that entityid, workflowid.
ReplyDeletePlease advice
How and where can we find the entityid and workflowid
ReplyDeleteHello Uday,
ReplyDeleteOn your record window (either entity or workflow) you can click on Copy a Link button. This will allow you to paste the URL of that record in notepad file or any other temporary location. From that URL you could identify the ID (Guid) of that record. I hope it is clear now.
Hello, and thanks for your post. I'm attempting to integrate my workflow GUID and custom entity GUID and wanted to see what I might be doing wrong.
ReplyDeleteShould I be adding the GUID like this? (I've removed code reference)
"EntityId" + "7bBF27441D-AF1F-4E7D-9F72-8C04B9EF5F55" + "/EntityId" +
"WorkflowId" + "7b1D1F8595-A49F-432C-B1B0-C34ECF2EF55D" + "/WorkflowId" +
And is that the only place I would input them?
thanks for your help.
Hello James,
ReplyDeleteYes, you just require to pass the values of these two parameters to startWorkflow Method.
Hi Ankit,
ReplyDeleteused the below code, however my workflow isnt firing can you please help?
function startWorkflow() {
var entityId = Xrm.Page.data.entity.getId();
var workflowProcessId = "54698A96-4926-4647-9A12-45DB4C2448B8"; //Workflow Guid.
var xml = "" +
"" +
"" +
Xrm.Page.context.getAuthenticationHeader() +
"" +
"" +
"" +
"" + entityId + "" +
"" + workflowProcessId + "" +
"" +
"" +
"" +
"";
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", xml.length);
xmlHttpRequest.send(xml);
}
Hi,
ReplyDeleteCan you please validate each following point?
1. Please validate the GUID value of workflow, is that correct?
2. Is the workflow activated?
3. Is it On Demand workflow?
Thanks,
Ankit
Thanks,
Ankit
not working crm2015
ReplyDeleteHello Rahul,
DeleteI have updated this post today and it should now work with CRM 2015.
thanks Ankit
ReplyDeleteThank you so much man.. Worked like a charm for 2015. :)
ReplyDeleteIs this JS code still IE-only, or will it now work with Chrome / Firefox, etc.?
ReplyDeleteHi,
ReplyDeleteIt now supports all browsers, it is no more IE-only since 24th Sep 2015.
Thanks,
Ankit
Hi,
ReplyDeletei have a requirement that i have to trigger a workflow from ribbon button which is already there. i have used this same code.this is not working. and the button which supposed to do the action that also not happening . can you please guide me.
Thanks
Hi Work Flow id will change for record to record and while debugging the script showing Guid Undefined
ReplyDeleteHi,
DeleteYou've specified that "The workflow id will change for record to record"
Can you please elaborate more on that?
Hi sir
ReplyDeletei am used same code in java script
occur some problem "
Failed to load resource: the server responded with a status of 500 (Internal Server Error)"
Hi Ankit
ReplyDeleteYour code works for me well when I fire workflow on same entity. If I try this to fire a workflow on account entity from a contact entity it doesn't work. Can I know why please?
Thanks
Arj
Hi AJ,
DeleteSorry for the delayed reply.
Can you let me know where exactly are you facing the issue? Because the below code works for me for updating the Account by triggering the workflow from Contact. Make sure the workflow is set to trigger On Demand.
function ContactEmailOnChange(executionContext) {
//Get the Account Id on Contact
var formContext = executionContext.getFormContext();
var accountAttribute = formContext.getAttribute("parentcustomerid");
if (accountAttribute != null && accountAttribute.getValue() != null) {
var accountId = accountAttribute.getValue()[0].id;
var entity = {
"EntityId": accountId // accountId
};
var WorkflowId = "b7f7ebd1-3a91-43a2-8f41-35ed28ee4dfc";
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/workflows(" + WorkflowId + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
Xrm.Utility.alertDialog("Success");
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
}
}