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.
Now, we are going use the following code in our JavaScript web resource to call the above workflow.
- function ExecuteWorkflow()
- {
-
- Xrm.Utility.showProgressIndicator("Executing Workflow....");
- var serverURL = Xrm.Page.context.getClientUrl();
- var contactID=Xrm.Page.data.entity.getId().substring(1, 37);
-
- var workflowID="E7C36B0F-DCAB-4440-9A0A-FC499A4E2A84";
-
- var data = {
- "EntityId": contactID
- };
-
- var req = new XMLHttpRequest();
- req.open("POST", serverURL + "/api/data/v8.2/workflows("+workflowID+")/Microsoft.Dynamics.CRM.ExecuteWorkflow", 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 ) {
- req.onreadystatechange = null;
- if (this.status == 200) {
- Xrm.Utility.closeProgressIndicator();
- } else {
- var error = JSON.parse(this.response).error;
- alert(error.message);
- Xrm.Utility.closeProgressIndicator();
- }
- }
- };
- req.send(JSON.stringify(data));
- }
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.
After that, we need to use this command in our button like the following:
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.
I hope it will help someone !!