How A Regular User Can Cancel Running Flow Instance Automatically?

Problem

Being an admin or IT person, end users might come to you and ask to stop the workflow or cancel the workflow process. There could any reason for that such as: 

  • Metadata entered by user in the record is wrong and he/she wants to recreate that record
  • Or approver is on vacation, so end user wants to cancel it or skip it
  • Or approval has been already taken offline, so no need of digital approval anymore 

Can a regular user find and cancel the running instance of the flow? No. Because he is not Flow Owner neither he is not a tenant admin. 

Can he at least trigger another FLOW that will automatically cancel the running instance? Well, let’s see.

Solution

Only Flow owner or Admin user can view run history, cancel or resubmit the instance. If we want to automate this, and get it triggered automatically then we need to create another FLOW.

Note
This configuration needs premium connector as we need to make an API call. Only one FLOW per user or PowerApps per user license is needed, which can be assigned to service account or admin account. Create the Cancellation FLOW with that service account only. OR you can make use of Logic Apps provided you have Azure Subscription.

Consider that you have a SharePoint lists called Tenants. You have Tenants Approval FLOW running on this list. You have Status column with one of the possible values as In Progress, Rejected, Approved and Cancelled. Now let’s follow below steps to configure automation of running flow instance. 

Provide user interface for End users to change the status of their records. You can either create PowerApps app or use default SharePoint list forms. I will leave this to you.

Change the trigger condition of Tenants Approval FLOW to as shown below,

@not(equals(triggerBody()?['ApprovalStatus']?['value'],'Cancelled'))

Create new FLOW with trigger as SharePoint item modified, configure it for same Tenant’s list. And set the trigger condition as shown below

@equals(triggerBody()?['ApprovalStatus']?['value'],'Cancelled')

This will make sure that whenever end user change the status of the record to Cancelled, only cancellation FLOW get triggered.

Now we must generate API URL to cancel the already running instance of main approval FLOW. Refer this article to understand how you can save URL of running instance in the record itself. We will need this URL to cancel it.

We have to modify the running FLOW instance URL a bit and generate it as shown in below format

https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/{environmentID}/flows/{flowID}/runs/{runID}/cancel?api-version=2016-11-01

Use below concat and replace function expression to generate the above URL, FlowInstanceURL is hyperlink column in Tenants list where we have stored running FLOW instance URL.

concat(replace(triggerOutputs()?['body/FlowInstanceURL'], 'https://asia.flow.microsoft.com/manage',
'https://api.flow.microsoft.com/providers/microsoft.processsimple'), '/cancel?api-version=2016-11-01')

Now as we have the API URL ready, we must make HTTP call to it. We will be using Azure AD HTTP Invoke action for that. Configure this action with below URLs and refer the screenshot below

Base Resource URL
https://api.flow.microsoft.com/
Azure AD Resource URI
https://service.flow.microsoft.com/

Add Azure HTTP Invoke action, configure it as shown below, method has to be POST, provide URL value from previous action

Well, that’s all we have to do. Now just go ahead and test with any record. Change the status to Cancelled, the cancellation flow will trigger and cancel the running instance of Main FLOW. 

Check out above configuration in details and action in this YouTube video.

Summary

This will reduce lot of administration work, will reduce workload on admin team. End users can cancel the flows whenever they want. Hope this will help you guys. Thanks for reading.


Similar Articles