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.
- var executeWorkflowRequest = new ExecuteWorkflowRequest()
- {
- WorkflowId = Guid.Parse("24dc5603-a117-4221-a7bb-5b6ed17a1810"), // Guid of workflow
- EntityId = Guid.Parse("B0A19CDD-88DF-E311-B8E5-6C3BE5A8B200") // Guid of record
- };
- 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
- POST[Organization URI]/api/data/v9.0/workflows(<Workflow Guid>)/Microsoft.Dynamics.CRM.ExecuteWorkflow HTTP/ 1.1
- Accept: application/json
- Content-Type: application/json;charset=utf-8
- OData-MaxVersion: 4.0
- OData-Version: 4.0
- {
- "EntityId": "<Entity Record Guid>"
- }
Making Request Using XmlHttpRequest
- var clientUrl = Xrm.Page.context.getClientUrl();
- var workflowId = "24dc5603-a117-4221-a7bb-5b6ed17a1810";
- var entityId = "B0A19CDD-88DF-E311-B8E5-6C3BE5A8B200";
- var requestUri = clientUrl + "/api/data/v9.0/workflows(" + workflowId + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";
- var xhr = new XMLHttpRequest();
- xhr.open("POST", requestUri, true);
- xhr.setRequestHeader("Accept", "application/json");
- xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
- xhr.setRequestHeader("OData-MaxVersion", "4.0");
- xhr.setRequestHeader("OData-Version", "4.0");
- xhr.onreadystatechange = function () {
- if (this.readyState == 4) {
- xhr.onreadystatechange = null;
- if (this.status == 200) {
- var result = JSON.parse(this.response);
- } else {
- var error = JSON.parse(this.response).error;
- }
- }
- };
- xhr.send("{\"EntityId\":\"" + entityId + "\"}");
Making Request In a Modern Way Using Fetch
- var clientUrl = Xrm.Page.context.getClientUrl();
- var workflowId = "24dc5603-a117-4221-a7bb-5b6ed17a1810";
- var entityId = "B0A19CDD-88DF-E311-B8E5-6C3BE5A8B200";
- fetch(
- clientUrl + "/api/data/v9.0/workflows(" + workflowId + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow",
- {
- body: "{\"EntityId\":\"" + entityId + "\"}",
- credentials: "same-origin",
- headers: {
- "Accept": "application/json",
- "Content-Type": "application/json; charset=utf-8",
- "OData-MaxVersion": "4.0",
- "OData-Version": "4.0"
- },
- method: "POST"
- })
- .then(response => console.log("Success:", response))
- .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.
- <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
- <entity name="workflow">
- <filter type="and">
- <condition attribute="name" operator="eq" value="Enter Workflow Name Here!" />
- </filter>
- </entity>
- </fetch>
Nice Article!! Very useful information to everyone thanks for sharing.
ReplyDeleteAzure DevOps Online Training
Azure DevOps Training
Azure DevOps Online Training in Hyderabad
Azure Devops Training Online