Execute Workflow using Web API in Dynamics 365

By | November 16, 2016

Introduction:

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
       //https://org.crm.dynamics.com/api/data/v8.2/workflows(“f0ca33cc-23fd-496f-80e1-693873a951ca”)/Microsoft.Dynamics.CRM.ExecuteWorkflow
        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;
                }
            }
        };
        req.send(JSON.stringify(data));

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

Conclusion

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 you can try User Adoption Monitor from Microsoft AppSource – Try today!

We would like to take a moment to introduce our new product Alerts4Dynamics to you. Alerts4Dynamics lets you schedule and manage alerts in Dynamics 365 CRM to notify users about the updates in CRM, due invoices, reminder to send quotes, etc. You can define target audience and send them priority based alerts via pop-ups, form notifications and emails. You can also view the log of read/dismissed alerts by users and also create alerts for multiple records by defining rules.

2 thoughts on “Execute Workflow using Web API in Dynamics 365

  1. Jeff Dodds

    Thanks for this it gave me a great starting point for a project I am on. I have extended it a bit to get the client URL from the Global Context instead of as a parameter and added the new (as of 9.x) Xrm.Utility.closeProgressIndicator() to both block the UI and give feedback when completed.

    One comment is that throwError utilizedin you sample in the catch statement itself threw an error as a non existent function.

    Reply
    1. inogic Post author

      Thank you for your input.
      “throwError” used in our code is another custom function used to show error message on UI. The code is as follows:

      /*Generic function for throwing an error*/
      function throwError(functionName, error) {
      var message=””;
      try {
      message=functionName+error;
      Xrm.Navigation.openAlertDialog(message);
      } catch (e) {
      alert(message);
      }
      }

      Let us know if you need any further assistance on this.

      Reply

Leave a Reply

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