Cancel Ongoing Flow Runs in Microsoft Dynamics 365 Dataverse: Complete Guide

We came across an issue that a lot of flow runs that were accidentally triggered based upon a mutation of a Dataverse/CRM entity action in a specific flow.

Microsoft does have a solution to cancel runs using the power automate template (Power automation cancel bulk template)

However, the issue is that this cancel around 5000 flow runs in 18 hours since there is a delay of around 10 seconds. If you remove the delay, the flow does not give the expected results.

Authorization

Now the question is how to fix this issue?

There is a flow api that allows you to cancel flow runs, but there are some steps to take to be able to do this.

Create an app registration in microsoft azure ad/entra with the permission 'Flows.Manage.All' and add the redirect url 'https://global.consent.azure-apim.net/redirect' under Authentication

Add a secret to the app registration write this down because you won't be able to see it anymore after creation.














In postman create a collection with the authorization type of 'Authorization Code'

The following information needs to be entered:

Callback URL: https://global.consent.azure-apim.net/redirect
Auth URL: https://login.microsoftonline.com/common/oauth2/authorize?resource=https://service.flow.microsoft.com
Access token URL: https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token
Client Id: The client id of the app registration.
Client Secret: The secret that was created for the app registration.
Scope: https://service.flow.microsoft.com/.default



You can use these settings to generate an access token. A prompt will appear to log in to the
environment, then login with your credentials once completed the JWT token will appear which you will need to add to your request.


Api Calls

Acquire all flow runs for specific flow that have the status running

Method = Get

https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/{Flow-Environment-Guid/flows/{Flow-Guid-Id}/runs?api-version=2016-11-01&$filter=Status eq 'running'


Cancel a flow run 
Method = Post

https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/{Flow-Environment-Guid}/flows/{Flow-Guid-Id}/runs/{Run Id}/cancel?api-version=2016-11-01


Note: The run id can be acquired from the previous call.



I also created a git repository to be able to do run this from code.














Comments

Popular posts from this blog

How to Implement Advanced Filtering in Azure Event Grid for More Granular Event Processing

How to Cancel Dynamics 365 Dataverse Order in 2024: Step-by-Step Guide