Solution for Dynamics 365 (triggering workflow from JavaScript)
The workflow should be on-demand process
First get the Workflow id using the below code
var workFlowName = "myWorkFlow";
var workFlowId = "";
var xmlData = Xrm.Page.context.getClientUrl() + '/XRMServices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=StateCode/Value eq 1 and ParentWorkflowId/Id eq null and Name eq \'' + workFlowName + '\'';
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", xmlData, false);
xmlHttp.send();
if (xmlHttp.status == 200) {
var result = xmlHttp.responseText;
workFlowId = //------ (write logic to parse workflow id from xmlHttp object)
}
Now Trigger the Work Flow
var functionName = "executeWorkflow >>";
var query = "workflows(" + workflowId.replace("}", "").replace("{", "") + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";
var data = {
"EntityId": accountId
};
var req = new XMLHttpRequest();
req.open("POST", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.1/" + query), true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function() {
if (this.readyState == 4 /* complete */ ) {
if (this.status == 204) {
//success callback this returns null since no return value available.
} else {
//error callback
}
}
};
req.send(JSON.stringify(data));
The above code works perfectly, hope it helps!