function OK() {
var entityId = Xrm.Page.data.entity.getId();
var workflowId = 'C652E4A2-3187-4526-8CA4-A4A4FD8F9EC3';
//Function for executing the workflow
if (entityId != null) {
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=\"schemas.xmlsoap.org/.../envelope\">";
requestMain += " <s:Body>";
requestMain += " <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";
requestMain += " <request i:type=\"b:ExecuteWorkflowRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">";
requestMain += " <a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <c:key>EntityId</c:key>";
requestMain += " <c:value i:type=\"d:guid\" xmlns:d=\"schemas.microsoft.com/.../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=\"schemas.microsoft.com/.../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", "schemas.microsoft.com/.../Execute&#8221");
//req.onreadystatechange = function () { };
req.send(requestMain);
alert("Workflow execution Completed.");
}
catch (e) {
alert(e.message.toString());
}
}
*This post is locked for comments
I have the same question (0)