Is it possible to use JavaScript to Execute a Real Time On-Demand Workflow (NOT and Asynchronous Workflow)
Using the code below, I am able to execute Asynchronous On-Demand Workflows.
However, I would like to execute a realtime workflow via JavaScript.
Is that possible? If yes, how would I do that via JavaScript?
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 "\"}");
IsDirty = formContext.data.entity.getIsDirty();
if (IsDirty) {
formContext.data.refresh().then(function () {
formContext.data.refresh(true);
}, null);
formContext.data.refresh(true);
}
else {
//formContext.data.refresh(true);
formContext.data.refresh().then(function () {
formContext.data.refresh(true);
}, null);
formContext.data.refresh(true);
}
} 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 "\"}");
IsDirty = formContext.data.entity.getIsDirty();
if (IsDirty) {
formContext.data.refresh().then(function () {
formContext.data.refresh(true);
}, null);
formContext.data.refresh(true);
}
else {
//formContext.data.refresh(true);
formContext.data.refresh().then(function () {
formContext.data.refresh(true);
}, null);
formContext.data.refresh(true);
}
}
} else if (OptVal == 100000001); {
formContext.getAttribute("ace_serviceaddressline1").setValue(null);
formContext.getAttribute("ace_serviceaddressline2").setValue(null);
formContext.getAttribute("ace_serviceaddresscity").setValue(null);
formContext.getAttribute("ace_serviceaddressstate").setValue(null);
formContext.getAttribute("ace_serviceaddresspostalcode").setValue(null);
formContext.getAttribute("ace_serviceaddresscountry").setValue("United States");
formContext.getAttribute("ace_serviceaddresslatitude").setValue(null);
formContext.getAttribute("ace_serviceaddresslongitude").setValue(null);
}
}