Avoid Infinite Loops In Power Automate

Introduction

 
PowerAutomate is the automation technology in the MSFT platform that lets you automate the business processes by letting you choose the pre-defined actions and configure them from the set of allowed connectors. This is no / low code way of developing and automating business processes. I say low code because if you are interacting with HTTP connectors, you still need to decorate your headers, when trying to use advanced options in actions you may write 'expressions' and OData queries to parse the result set.  
 
Now. let's try to understand connector in laymen's terms. From the Microsoft documentation, "It is a proxy or wrapper around an API that lets underlying service to talk to Microsoft, Power Automate, Power Apps and Logic Apps."
 
Consider a scenario. You are visiting a country for example let's say 'Spain'. Let's assume, that you don't know the local language Spanish. In this internet world, we can use smartphones and use the 'Translator App'. What this Translator app does is, takes your voice or text and then translates it to the native language you chose, in this case 'Spanish. Now the 'Translator App' in your smartphone, helping you to interact with local guides/persons to enjoy your visit and finish your work there. 
 
A connector is exactly doing the same thing. It translates your requirement so that Power Platform can understand and perform the tasks. It acts as a medium that lets your application talk to MSFT Power Platform(power apps, power automate) and Logic Apps using your M365 account. There are 2 types of connectors 'Standard' and 'Premium'. Standard connectors that come as free with your subscription, and premium connectors involve additional pricing depending upon the type of service your application interacts with. Please go to the reference section to learn more about connectors.   
 
The important thing to remember is the limitations that come with the Power Automate. Based on the subscriptions (E1, E3, E5, etc) the flow API calls are limited per 24hour duration. For example, if you are on an E1 subscription with Office 365 services, you have a 2000 API run limit per 24-hour time frame.
 
Now the question comes what is an API call? Every step, every query, and Every action that runs in your power platform is an API call to M365 services. You can refer to request limits and limitations for the power platform in the references section. Since in this article I am using only 2 actions, compose, update item, each flow completion is considered as 2 API runs. 
 
You may notice I have used Power Automate and Flow interchangeably. MSFT updated the terminology and says FLOW is now Power Automate. To avoid confusion, I refer to Power Automate as the technology or service that lets you automate your business process and 'FLOW' is your design you implement to configure your process. 
 
In this article, let's understand how to avoid infinite loops for flow (avoiding retriggering the flow again, again, again, and again). For simplicity, I have created a custom list and created a simple flow that has only one action ‘SharePoint Update Item’.
 
Screen capture of Custom list
 
Avoid Infinite Loops Power Automate
 
It contains the following fields,
  • Registration Name: of column type 'single line of text'
  • Email:  of column type 'single line of text'
  • Interests: of column type 'Choice'  . The choice values are Power Virtual Agent, Power Apps, Power Automate, SPFx
Screen capture of the Power Automate Design
 
Avoid Infinite Loops Power Automate
 
The scenario here is to start a flow when an item is created are modified. Maybe this could be your use case, where your flow needs to run when an item is created or modified. If you see the flow checker, the flow is intelligent to warn us. There aren’t any errors, but there is a warning.
 
Avoid Infinite Loops Power Automate
 
It says that actions in this flow may run into an infinite loop. please ensure you add appropriate conditions. Let’s see how we can tackle this.
 
Important Point
 
Point to note that flow is running under the context of the account that it is configured with. It is a best practice to use a service account to configure the flow. A service account is just like a normal user account that has a standard subscription. In this scenario, I am using svco365test@cts229051.onmicrosoft.com. the item gets updated by the service account when this flow is run.
 
Avoid Infinite Loops Power Automate
 

Steps

 
Step 1
 
Try adding ‘complex action. We will be cleaning up this later. This is needed to generate expressions for the trigger condition.
 
Avoid Infinite Loops Power Automate
 
Step 2
 
Now configure the compose, by entering the expression. Use the ‘equals’ operator and look for the ‘modified by email’ field under dynamic content,
 
Avoid Infinite Loops Power Automate
 
Step 3
 
Now you are comparing the last modified account by email is equal to the service account or not. the idea is if this account last modified by its service account then do not trigger. For this, you will decorate the equals operator with not operator. the final expression should look like this,
 
not(equals(triggerOutputs()?['body/Editor/Email'],'svco365test@yourcompanydomain.onmicrosoft.com'))
 
Avoid Infinite Loops Power Automate
 
Step 4
 
Now copy this expression, and go to trigger settings, and paste this under the trigger condition. Please make sure you decorate condition with @.
 
Avoid Infinite Loops Power Automate
 
Avoid Infinite Loops Power Automate
 
Step 5
 
Save and test the flow. the observation here is that, as a normal user whenever there is an item creation or item update the flow triggers. As a service account user, whenever there is an item creation or item update, the flow will not trigger.
 
Below are the screen captures for references,
 
Avoid Infinite Loops Power Automate
As a normal user, I have created an item and I have updated an item. Total there are 2 flow runs. Also as a service account user, I have created an item. Since we have put conditional triggers there are only 2 runs consumed.
 
Avoid Infinite Loops Power Automate
 
You can also see the flow analytics by going to the flow maker portal at https://flow.microsoft.com and then selecting the flow clicking on ‘Analytics’. This will help in analyzing the flow and come up with a better design. 
 
Avoid Infinite Loops Power Automate
 
This page will give the overall runs, and actions that are used, and 28 days history. Note that the analytics history beyond 28 days is not available. 
 
Avoid Infinite Loops Power Automate
 

Conclusion

 
Thus, in this article, we have seen how to save the flow triggers and avoid infinite loops, and monitor your flow runs.
 
References
  • https://docs.microsoft.com/en-us/power-platform/admin/api-request-limits-allocations
  • https://docs.microsoft.com/en-us/connectors/
  • https://us.flow.microsoft.com/en-us/connectors/
  • https://www.youtube.com/watch?v=oKN4_5o2NUA&t=1s
  • https://docs.microsoft.com/en-us/power-platform/admin/analytics-flow#who-can-view-these-reports