Thursday, 3 February 2022

Calling Dynamics 365 Workflows Using C# Or JavaScript

 In Dynamics 365, workflows are very useful to implement business logic without writing any code. If we need to trigger workflows from our code, there are ways provided in both Organization Service and WebAPI. Let's explore how to achieve these.


Calling Workflow Using C#

ExecuteWorkflowRequest  triggers workflows using C# or any other .NET language.

  1. var executeWorkflowRequest = new ExecuteWorkflowRequest()  
  2. {  
  3.   WorkflowId = Guid.Parse("24dc5603-a117-4221-a7bb-5b6ed17a1810"), // Guid of workflow  
  4.   EntityId = Guid.Parse("B0A19CDD-88DF-E311-B8E5-6C3BE5A8B200"// Guid of record  
  5. };              
  6. var executeWorkflowResponse =(ExecuteWorkflowResponse)orgService.Execute(executeWorkflowRequest); 

To test the above snippet, you can feel free to use Dynamics 365 Console Caller.

Calling Workflow Using JavaScript Ajax

ExecuteWorkflow Action lets us trigger a Workflow using WebAPI, which is a Bound Action so we have to pass GUID as the first parameter in URI. Our request should be in the below format.

HTTP Request Format

  1. POST[Organization URI]/api/data/v9.0/workflows(<Workflow Guid>)/Microsoft.Dynamics.CRM.ExecuteWorkflow HTTP/ 1.1  
  2. Accept: application/json  
  3. Content-Type: application/json;charset=utf-8  
  4. OData-MaxVersion: 4.0  
  5. OData-Version: 4.0  
  6.   
  7. {  
  8.   "EntityId""<Entity Record Guid>"  

 Making Request Using XmlHttpRequest

  1. var clientUrl = Xrm.Page.context.getClientUrl();  
  2. var workflowId = "24dc5603-a117-4221-a7bb-5b6ed17a1810";  
  3. var entityId = "B0A19CDD-88DF-E311-B8E5-6C3BE5A8B200";  
  4.   
  5. var requestUri = clientUrl + "/api/data/v9.0/workflows(" + workflowId + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";  
  6.   
  7. var xhr = new XMLHttpRequest();  
  8. xhr.open("POST", requestUri, true);  
  9. xhr.setRequestHeader("Accept""application/json");  
  10. xhr.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  11. xhr.setRequestHeader("OData-MaxVersion""4.0");  
  12. xhr.setRequestHeader("OData-Version""4.0");  
  13. xhr.onreadystatechange = function () {  
  14.     if (this.readyState == 4) {  
  15.         xhr.onreadystatechange = null;  
  16.         if (this.status == 200) {  
  17.             var result = JSON.parse(this.response);  
  18.         } else {  
  19.             var error = JSON.parse(this.response).error;  
  20.         }  
  21.     }  
  22. };  
  23. xhr.send("{\"EntityId\":\"" + entityId + "\"}"); 

Making Request In a Modern Way Using Fetch

  1. var clientUrl = Xrm.Page.context.getClientUrl();  
  2. var workflowId = "24dc5603-a117-4221-a7bb-5b6ed17a1810";  
  3. var entityId = "B0A19CDD-88DF-E311-B8E5-6C3BE5A8B200";  
  4.   
  5. fetch(  
  6.     clientUrl + "/api/data/v9.0/workflows(" + workflowId + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow",  
  7.     {  
  8.         body: "{\"EntityId\":\"" + entityId + "\"}",  
  9.         credentials: "same-origin",  
  10.         headers: {  
  11.             "Accept""application/json",  
  12.             "Content-Type""application/json; charset=utf-8",  
  13.             "OData-MaxVersion""4.0",  
  14.             "OData-Version""4.0"  
  15.         },  
  16.         method: "POST"  
  17.     })  
  18.     .then(response => console.log("Success:", response))  
  19.     .catch(error => console.error("Error:", error)); 

But Where Can I Get My Workflow GUID?

Execute the below fetchXml using FetchXml Tester Online after replacing your Workflow name and grab GUID from the JSON response.

  1. <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">  
  2.   <entity name="workflow">  
  3.     <filter type="and">  
  4.       <condition attribute="name" operator="eq" value="Enter Workflow Name Here!" />  
  5.     </filter>  
  6.   </entity>  
  7. </fetch> 

1 comment:

Power Apps Drag & Drop Kanban code sample

  Introduction: The Kanban Board App Developers at a software company use the Kanban board to show their progress on development tasks and b...