cancel
Showing results for 
Search instead for 
Did you mean: 
rampprakash

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 🙂