Prevent MS Flow Retrigger on Item Creation/Modification

Introduction

MS Flow is one of the key Apps on Office 365, which helps in quickly automating tasks and easily building complex business logic on list item level events. With MS Flow adding more actions and triggers, there is almost no need to create Event Receivers anymore. But one significant feature that Event Receivers offered was to disable the event firing when we are updating the same list item with the event receiver.

But MS Flow doesn't provide the provision to stop a trigger.

Problem

Consider the below example where we have a list with 3 columns.

Additions

A simple flow can be configured which triggers whenever an item is created or modified, which will update ColumnSum with a sum of ColumnOne and ColumnTwo. The MS Flow steps may look like below.

When an item is created or modified

This Flow keeps triggering itself infinitely, because of the 'Update Item' action.

Solution

As MS Flow doesn't provide any provision to disable the re-trigger, we need to bring in a workaround in our flow structure that stops the Update Item' action, when the update happens from the Flow itself. There are many workarounds but most of them are specific to the logic of the flow and use cases.

Here is a generic workaround that will work for most of the scenarios.

Here is how the high-level flow structure should look like

  1. Trigger: 'When an item is created or modified'
  2. Action: Use 'Send an HTTP request to SharePoint' to make a call to the SharePoint REST API to get the last 2 versions of the list item
  3. We maintain one column called 'FlowFlag' in the list to track if the update event is triggered by the Flow itself or not
  4. Compare the value of the 'FlowFlag' column from the last 2 versions of the list item
  5. If the value is the same, then proceed with the Flow, if they are not the same which means Flow has triggered, then terminate the Flow
  6. And whenever we update the list item, also update the value of the FlowFlag column by incrementing it by 1, so that its value is different compared to the previous version and the next Flow trigger will not update the list item again.

Building the flow with a workaround

Create a column in the list of type numbers, called 'FlowFlag'. Then proceed with creating the Flow as mentioned below.

  1. Choose the trigger as 'When an item is created or modified'
    When an item is created or modified
  2. Add an action: 'Send an HTTP request to SharePoint', and update the properties with the appropriate values.
  3. Site address: Select the site collection where your list is present.
  4. Method: Enter as GET
  5. Uri: _api/web/lists/getbytitle('Additions')/items(@{triggerBody()?['ID']})/versions?$top=2 - This is a normal SharePoint REST API, where we are reading the list item (using ID) and calling versions and requesting only the top 2 versions.
  6. Headers: Enter the Key as 'accept' and the value as 'application/json;odata=nometadata'
    Get versions
  7. And rename the step as 'GetVersions' to make it easy to understand.
  8. Next, add the Select Step, to parse the output of the previous step and prepare an array with only the FlowFlag value.
  9. From: This expects an array. Add the expression - body('GetVersions')?['value'] for this property.
  10. Map: Enter the key as a flag and value as an expression - item()?['FlowFlag'].
    Select
  11. Add a Condition step next, and check the value of 'FlowFlag' in the first and last rows from the Select step.Condition
  12. In the Yes section of the condition, add steps to perform the actual operations or add your processing logic. In the No section, terminate the flow with the status as success. We don't want to do anything when the flow itself has triggered the action.
    Add steps

Enable versioning on the list

As this workaround relies on the versions of the list item, the List Versioning should be enabled. Go to the List Settings -> Versioning Settings and enable it.

Item Version history

Maintain at least the last 2 major versions so that we can check the versions in the flow.

Summary

With this workaround, we can handle the infinite triggering problem of MS flow when it is updating the same list item. This flow could still trigger twice for an item add/update action, but it stops after the second trigger due to the condition check. Hope this helps.


Similar Articles