Hi Andreas,
thanks for your answer. I tried your script but unfortunately it is creating a strange error:
This is the code I used:
// Main function to be called from Ribbon button to execute the workflow
function ExecuteWorkflowFromRibbonButton(workflowId) {
//Get workflow id by passing the workflow name as parameter
var workflowId = workflowId;
if (workflowId != null) {
// Get record id
var entityId = Xrm.Page.data.entity.getId();
//Function for executing the workflow
startWorkflow(entityId, workflowId);
}
}
function startWorkflow(entityId, workflowId) {
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\">" + workflowId + "</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.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;
}