How To Stop MS Flow From Re-Triggering On An Item Created/Modifed Trigger

If an MS Flow with an 'Item is created or modified' trigger tries to update the same list item, it re-triggers itself. This article shows a workaround on how to avoid the endless recursive calls.

Introduction

 
MS Flow is one of the key Apps on Office 365, which helps in quickly automating tasks and easily build 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 which 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.
 
How To Stop MS Flow From Re-Triggering On An Item Created/Modifed Trigger
 
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.
 
How To Stop MS Flow From Re-Triggering On An Item Created/Modifed Trigger
 
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 which 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 which will work for most of the scenarios.
 

How do we do it?

 
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 get make a call to the SharePoint REST API for getting 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 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 number, called 'FlowFlag'. Then proceed with creating the Flow as mentioned below
  1. Choose the trigger as 'When an item is created or modified'

    How To Stop MS Flow From Re-Triggering On An Item Created/Modifed Trigger

  2. Add an action - 'Send an HTTP request to SharePoint', and update the properties with the appropriate values
    Site Address: Select the site collection where your list is present
    Method: Enter as GET
    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.
    Headers: Enter the Key as 'accept' and value as 'application/json;odata=nometadata'

    How To Stop MS Flow From Re-Triggering On An Item Created/Modifed Trigger

    And rename the step as 'GetVersions' to make it easy to understand.

  3. Next, add the Select Step, to parse the output of the previous step and prepare an array with only the FlowFlag value
    From: This expects an array. Add the expression - body('GetVersions')?['value'] for this property
    Map: Enter the key as flag and value as an expression - item()?['FlowFlag']

    How To Stop MS Flow From Re-Triggering On An Item Created/Modifed Trigger

  4. Add a Condition step next, and check the value of 'FlowFlag' in the first and last rows from the Select step

    How To Stop MS Flow From Re-Triggering On An Item Created/Modifed Trigger

  5. In the Yes section of the condition, add steps to perform the actual operations or add your processing logic. And 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.

    How To Stop MS Flow From Re-Triggering On An Item Created/Modifed Trigger

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.

How To Stop MS Flow From Re-Triggering On An Item Created/Modifed Trigger

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.