Executing Workflow From Command Button Using Web API

Introduction

In our earlier sample, we discussed how to call actions using Web API, and today we are going to share sample code to execute workflow from command button using Web API.

Requirement

Let’s say we got some requirement to validate some logic on click of the command button and need to send an email using workflow. Let’s see how we can do that.

Solution

First, we need to create a custom solution, in our solution we are going to add the workflow which we want to call using Web API, our entity (we are using Contact entity for demo) and will create a JavaScript web resource (We will be calling this web resource from command button).

We have created a workflow on contact entity which is set to run as On Demand and we can copy its GUID from the URL like the following.

Executing Workflow From Command Button Using Web API
 
Now, we are going use the following code in our JavaScript web resource to call the above workflow.
  1. function ExecuteWorkflow()  
  2. {  
  3.     //show indication to user  
  4.     Xrm.Utility.showProgressIndicator("Executing Workflow....");  
  5.     var serverURL = Xrm.Page.context.getClientUrl();  
  6.     var contactID=Xrm.Page.data.entity.getId().substring(1, 37);  
  7.     //workflow id that we captured above  
  8.     var workflowID="E7C36B0F-DCAB-4440-9A0A-FC499A4E2A84";  
  9.        
  10.      var data = {  
  11.         "EntityId": contactID  
  12.     };  
  13.     
  14.     var req = new XMLHttpRequest();  
  15.     req.open("POST", serverURL + "/api/data/v8.2/workflows("+workflowID+")/Microsoft.Dynamics.CRM.ExecuteWorkflow"true);  
  16.     req.setRequestHeader("Accept""application/json");  
  17.     req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  18.     req.setRequestHeader("OData-MaxVersion""4.0");  
  19.     req.setRequestHeader("OData-Version""4.0");  
  20.     req.onreadystatechange = function() {  
  21.         if (this.readyState == 4 /* complete */ ) {  
  22.             req.onreadystatechange = null;  
  23.             if (this.status == 200) {  
  24.                  Xrm.Utility.closeProgressIndicator();  
  25.             } else {  
  26.                 var error = JSON.parse(this.response).error;  
  27.                 alert(error.message);  
  28.                 Xrm.Utility.closeProgressIndicator();  
  29.             }  
  30.         }  
  31.     };  
  32.      req.send(JSON.stringify(data));  
  33. }  

In the above code we are using showProgressIndicator to display a notification to use, earlier there was no supported way to show these types of notifications and we used to write unsupported code to show notification messages. Next we are adding the parameter required for ExecuteWorkflow action, so we need to pass entity ID and we are using workflow ID in our Web API request. We need to hide notification messages using closeProgressIndicator.

Now, we can add a button on contact entity. We can add button easily using Ribbon Workbench. This tool is also available on the XrmToolBox, you can check our earlier post for how to add button.

First, we will add a command that we will be using in our command button like the following, we need to select our JavaScript web resource that we have created above and the name of the method in the script resource.

Executing Workflow From Command Button Using Web API
 
After that, we need to use this command in our button like the following:
 
Executing Workflow From Command Button Using Web API 
 
After publishing our changes we should be able to see Call Workflow button in contact entity form and once clicked, it should show the message like the following and should call workflow.
 
Executing Workflow From Command Button Using Web API 
 
I hope it will help someone !! 


Similar Articles
HIMBAP
We are expert in Microsoft Power Platform.