The code below executes several Asynchronous On-Demand Workflows within the appropriate if/else section.
I was forced to hardcode the Guid of each workflow directly in the code due to time constraints.
Ideally, I would like to get the Guid of the workflow by its name.
How would I do that exactly within the context of the code below?
function setOpportunityServiceAddress(executionContext) {
var formContext = executionContext.getFormContext();
var CurrentRecordId = formContext.data.entity.getId();
CurrentRecordId = CurrentRecordId.replace("{", "").replace("}", "");
var customer = formContext.getAttribute("ace_customer").getValue();
var customerType = customer[0].entityType;
var OptVal = formContext.getAttribute("ace_serviceaddress").getValue();
var WkflowId = "";
var clientUrl = "";
var requestUri = "";
var xhr = "";
var IsDirty = "";
if (OptVal == 100000000) {
if (customerType == "contact") {
//WkflowId = GetWorkflowIDByURL(executionContext, "/api/data/v9.1/workflows?$select=workflowidunique&$filter=(name eq 'Opportunity - (On Demand) Copy Related Contact Address to Service Address' and statuscode eq 2)&$top=1");
WkflowId = "fc67a661-9e41-49b5-b1ca-79eaaf9eadd4";
clientUrl = formContext.context.getClientUrl();
requestUri = clientUrl "/api/data/v9.0/workflows(" WkflowId ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";
xhr = new XMLHttpRequest();
xhr.open("POST", requestUri, true);
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.setRequestHeader("OData-MaxVersion", "4.0");
xhr.setRequestHeader("OData-Version", "4.0");
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
xhr.onreadystatechange = null;
if (this.status == 200) {
var result = JSON.parse(this.response);
}
else {
var error = JSON.parse(this.response).error;
}
}
};
xhr.send("{\"EntityId\":\"" CurrentRecordId "\"}");
} else if (customerType == "account") {
//WkflowId = GetWorkflowIDByURL(executionContext, "/api/data/v9.1/workflows?$select=workflowidunique&$filter=(name eq 'Opportunity - (On Demand) Copy Related Account Address to Service Address' and statuscode eq 2)&$top=1");
WkflowId = "9e5b668c-2feb-4d92-abfa-5af7194a02df";
clientUrl = formContext.context.getClientUrl();
requestUri = clientUrl "/api/data/v9.0/workflows(" WkflowId ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";
xhr = new XMLHttpRequest();
xhr.open("POST", requestUri, true);
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.setRequestHeader("OData-MaxVersion", "4.0");
xhr.setRequestHeader("OData-Version", "4.0");
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
xhr.onreadystatechange = null;
if (this.status == 200) {
var result = JSON.parse(this.response);
}
else {
var error = JSON.parse(this.response).error;
}
}
};
xhr.send("{\"EntityId\":\"" CurrentRecordId "\"}");
}
} else if (OptVal == 100000001); {
////DO SOME STUFF
}
}