Trigger/Execute workflow using javascript in CRM 2011/CRM 2013

Here is the code to execute the crm workflow via javascript.

// Main function to be called from Ribbon button to execute the workflow
function ExecuteWorkflowFromRibbonButton() {
//Get workflow id by passing the workflow name as parameter
var workflowId = GetWorkflowIDByName(“Test Workflow”);
if (workflowId != null) {
// Get record id
var entityId = Xrm.Page.data.entity.getId();
//Function for executing the workflow
startWorkflow(entityId, workflowId);
}
}

function startWorkflow(entityId, workflowProcessId) {
try {
var OrgServicePath = “/XRMServices/2011/Organization.svc/web”;
var serverUrl = “”;
if (typeof GetGlobalContext == “function”) {
var context = GetGlobalContext();
serverUrl = context.getServerUrl();
}
else {
if (typeof Xrm.Page.context == “object”) {
serverUrl = window.location.protocol + “//” + window.location.host + “//” + Xrm.Page.context.getOrgUniqueName();
//alert(serverUrl );
// serverUrl = Xrm.Page.context.getServerUrl();
//alert(serverUrl );
}
else { throw new Error(“Unable to access the server URL”); }
}
if (serverUrl.match(/\/$/)) {
serverUrl = serverUrl.substring(0, serverUrl.length – 1);
}

serverUrl = serverUrl + OrgServicePath;
var requestMain = “”
requestMain += “<s:Envelope xmlns:s=\”http://schemas.xmlsoap.org/soap/envelope/\”>”;
requestMain += ” <s:Body>”;
requestMain += ” <Execute xmlns=\”http://schemas.microsoft.com/xrm/2011/Contracts/Services\” xmlns:i=\”http://www.w3.org/2001/XMLSchema-instance\”>”;
requestMain += ” <request i:type=\”b:ExecuteWorkflowRequest\” xmlns:a=\”http://schemas.microsoft.com/xrm/2011/Contracts\” xmlns:b=\”http://schemas.microsoft.com/crm/2011/Contracts\”>”;
requestMain += ” <a:Parameters xmlns:c=\”http://schemas.datacontract.org/2004/07/System.Collections.Generic\”>”;
requestMain += ” <a:KeyValuePairOfstringanyType>”;
requestMain += ” <c:key>EntityId</c:key>”;
requestMain += ” <c:value i:type=\”d:guid\” xmlns:d=\”http://schemas.microsoft.com/2003/10/Serialization/\”>” + entityId + “</c:value>”;
requestMain += ” </a:KeyValuePairOfstringanyType>”;
requestMain += ” <a:KeyValuePairOfstringanyType>”;
requestMain += ” <c:key>WorkflowId</c:key>”;
requestMain += ” <c:value i:type=\”d:guid\” xmlns:d=\”http://schemas.microsoft.com/2003/10/Serialization/\”>” + workflowProcessId + “</c:value>”;
requestMain += ” </a:KeyValuePairOfstringanyType>”;
requestMain += ” </a:Parameters>”;
requestMain += ” <a:RequestId i:nil=\”true\” />”;
requestMain += ” <a:RequestName>ExecuteWorkflow</a:RequestName>”;
requestMain += ” </request>”;
requestMain += ” </Execute>”;
requestMain += ” </s:Body>”;
requestMain += “</s:Envelope>”;

var req = new XMLHttpRequest();
req.open(“POST”, serverUrl, 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”, “http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute&#8221;);
//req.onreadystatechange = function () { };
req.send(requestMain);

alert(“Workflow execution Completed.”);
}
catch (e) {
alert(e.message.toString());
}

}
//Function to get the workflow id
function GetWorkflowIDByName(wfName) {
//debugger;
var objFilteredWF = null;
var serverUrl = Xrm.Page.context.getServerUrl();
//Prepare ODATA query to fetch WF GUID by WF Name
var odataSelect = serverUrl + “/xrmservices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=ActiveWorkflowId/Id ne null and Name eq ‘” + wfName + “‘”;
$.ajax({
type: “GET”,
contentType: “application/json; charset=utf-8”,
datatype: “json”,
url: odataSelect,
beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader(“Accept”, “application/json”); },
success: function (data, textStatus, XmlHttpRequest) {
objFilteredWF = data.d;
},
error: function (XmlHttpRequest, textStatus, errorThrown) { alert(‘OData Select Failed: ‘ + odataSelect); }
});
var wfID = null;
if (objFilteredWF != null && objFilteredWF.results != null && objFilteredWF.results.length != 0 && objFilteredWF.results[0].WorkflowId != null) {
wfID = objFilteredWF.results[0].WorkflowId;
}
return wfID;

}

Namaste 🙂

Megh

Leave a comment