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.

Cut short 90% of your manual work and repetitive data entry!

Get 1 Click apps and say goodbye to all repetitive data entry in CRM –
Click2Clone – Clone/Copy Dynamics 365 CRM records in 1 Click
Click2Export – Export Dynamics 365 CRM Report/CRM Views/Word/Excel template in 1 Click
Click2Undo – Undo & Restore Dynamics 365 CRM data in 1 Click

4 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.

    1. inogic

      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.

  2. Julian

    Is there an option from the new web api to deactivate a workflow (process) that is active?

    1. Inogic

      Hi Julian,

      Yes, you can use “Xrm.WebApi.updateRecord” to update the status and status reason to deactivate the workflow.

      Please refer the below code to deactivate the workflow using “Xrm.WebApi.updateRecord”:

      //Function to call on onchange
      onchange(): void {
      let functionName: string = “onchange”;

      try{
      // define the data to update a record
      let data =
      {
      “statecode”: 0,

      “statuscode”: 1,
      }
      // update the workflow status
      Xrm.WebApi.updateRecord(“workflow”, “39760aac-e333-4ea7-b69f-8bf82503a401”, data).then(
      function success(result) {
      },
      function (error) {
      //Show alert
      Xrm.Navigation.openAlertDialog({ title: “Error”, text:error.message});
      }
      );
      }

      catch(ex)
      {
      //Show alert
      Xrm.Navigation.openAlertDialog({ title: “Error”, text:ex.message});
      }
      }

      You can refer the below link for more details on “Xrm.WebApi.updateRecord”:
      https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/updaterecord

      Hope this helps!
      Thanks

Comments are closed.