We use HTML Web Resource objects to create buttons that we can place on CRM forms to execute Workflows (called by the Workflow ID in the HTML). However, even if the Workflow itself is real-time, execution via HTML button does not show the results, and users need to refresh the page manually after clicking any of these buttons. (We've tested the workflows, they do refresh the data in real time and show the desired changes immediately upon execution when called via the Flows menu. So the issue is not with the Workflow itself.)
For example, we have a workflow on a custom entity that sets 8 different Boolean fields to Yes for a given client. It's a timesaver for data entry when all conditions apply to a given client.
Here is the HTML we currently use, can anyone suggest additional HTML (or other solution for a Web Resource) to have the flow execute in real time and show the results without a manual user refresh?
--------------------------------------
<html><head>
<script type="text/javascript">
function executeWorkflow() {
console.log(
"Record ID:" +
window.parent.Xrm.Page.data.entity.getId()
);
var entity = {
EntityId: window.parent.Xrm.Page.data.entity.getId() // recordId
};
var WorkflowId = "F5B6A605-0224-47D7-BC3F-DA187D7D4A51";
var req = new XMLHttpRequest();
req.open(
"POST",
window.parent.Xrm.Page.context.getClientUrl() +
"/api/data/v9.0/workflows(" +
WorkflowId +
")/Microsoft.Dynamics.CRM.ExecuteWorkflow",
true
);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
// if (this.status === 200) {
// window.parent.Xrm.Utility.alertDialog("Success");
} else {
// window.parent.Xrm.Utility.alertDialog("Alert");
// }
}
};
req.send(JSON.stringify(entity));
}
</script>
<meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta></head>
<body dir="LTR" onfocusout="parent.setEmailRange();" style="overflow-wrap: break-word;" lang="en-US">
<button onclick="executeWorkflow()" type="button">
All Eligible
</button>
</body></html>
--------------------------------------------