Execute Workflow using Web API in Dynamics 365

By | November 16, 2016


In every release, Microsoft comes up with some new and interesting features. With this Dynamics 365 release, the WEB API platform has been enhanced further. Microsoft has introduced many actions using Web API. Here we will see one of those new actions introduced in Dynamics 365 i.e. “ExecuteWorkflow”.

Until Dynamics CRM 2016 Update 1 (8.1), ExecuteWorkflowRequest was not available using Web API. But with Dynamics 365 “ExecuteWorkflow” is now available to us. Now we can execute the workflow using Web API.

Below is the code using which you can execute workflow.

function executeWorkflow(accountId, workflowId, clientUrl) {
    var functionName = "executeWorkflow >>";
    var query = "";
    try {

        //Define the query to execute the action
        query = "workflows(" + workflowId.replace("}", "").replace("{", "") + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";

        var data = {           
                "EntityId": accountId                         

        //Create request
       // request url
        var req = new XMLHttpRequest();
        req.open("POST", clientUrl + "/api/data/v8.2/" + query, true);
        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 /* complete */) {
                req.onreadystatechange = null;

                if (this.status == 200) {
                    //success callback this returns null since no return value available.
                    var result = JSON.parse(this.response);

                } else {
                    //error callback
                    var error = JSON.parse(this.response).error;

    } catch (e) {
        throwError(functionName, e);


There are a lot of cases where you add ribbon button to personalize the user experience but behind the scene you are actually executing a workflow. You can now go ahead and get those done through the “ExecuteWorkflow” action available. We will explore the other actions in later blogs.

Now u can try User Adoption Monitor from Microsoft AppSource – Try today!

Leave a Reply

Your email address will not be published. Required fields are marked *