How to Call Workflow using JavaScript in Dataverse
Implementation Steps:
Consider a Scenario, i need to Call an On-Demand Workflow on Create of an Account,
Usually when we tried to call a workflow (without On-Demand) we can directly call the workflow by Selecting Respective Stages in Process. To Call On-Demand Workflow, try calling below JavaScript
function triggerOnDemandWorkflow(executionContext) {
var formContext = executionContext.getFormContext();
var entityId = formContext.data.entity.getId();
var workflowId = "WORKFLOWGUID";
var query = "";
try {
query = "workflows(" + workflowId.replace("{", "").replace("}","") + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";
var data = {
"EntityId": entityId
};
//Create a request
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/" + query, false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 200) {
var result = JSON.parse(this.response);
} else {
Xrm.Utility.alertDialog("Error Occured!!. Please contact Administrator.");
}
}
};
req.send(JSON.stringify(data));
} catch (e) {
Xrm.Utility.alertDialog(e);
}
}
You need to Replace WORKFLOWGUID with your GUID.
Steps to Take GUID:
1. Navigate to your environment.
2. Click Gear Icon at the Top
3. Select Advance settings
4. Select Process
5. Open the Workflow which you have created
6. Copy the URL and Get only GUID from there.
That's it 🙂
Comments
-
How to Call Workflow using JavaScript in Dataverse
For anyone that needs help, some steps were left out,
- Navigate to your environment.
- Click Gear Icon at the Top
- Select Advance settings
- Click Settings, choose Customizations
- Choose Customize the System
- Choose Process from the navigation tree
- Open the Workflow which you have created
- Copy the URL and Get only GUID from there.
Alternatively,
Choose Solutions from the left NAV in Power Apps
Choose the Default Solution
choose Processes form the nav tree
Edit the workflow
copy the URL
*This post is locked for comments