How to Prevent Infinite Loops in Power Automate Flows for Dynamics 365 CE

By - November 21, 2019

A common issue with using Power Automate Flows on Dynamics 365 CE records is inadvertently causing infinite loops. The workflows in the old engine internal to D365 have the advantage of basic infinite loop prevention, but a Power Automate Flow can loop forever, wasting all of your monthly Flow runs at once. Fortunately, we can prevent infinite loops with some basic conditional logic and the Terminate step.

For this example, I will add an infinite loop check to a Flow meant to synchronize the timezone users select in their Personalization Settings and the timezone selected on their user Bookable Resource record. For more information regarding the functionality of the example Flow and the paired Flow causing the infinite loop, please reference blog.

Without the loop check, and assuming all Bookable Resources are User resource type, the Flow is only two basic steps:

  1. When a record is updated trigger on the Bookable Resource record. It is set with the “timezone” attribute filter to prevent unnecessary runs and avoid unanticipated infinite loops with other automation.
  2. Update a Record action on the User Settings entity. This is where the Personalization Settings selections in the user interface are stored, and is identified with the User lookup from Bookable Resource. In this case, only the timezone field is updated in this step.

This works just fine by itself, however this Flow is in a pair. This Flow triggers when a Bookable Resource timezone is updated and it updates the value in the User Settings. The other Flow in the pair triggers when the User Settings timezone is updated to update the value in Bookable Resource – causing an infinite loop as shown below:

To avoid this, we add a condition to the process to only update the record if the values are not already in sync, thereby not triggering another Flow.

An example of this process is shown below in red, starting with a user manually updating a timezone on a Bookable Resource. The Flow then checks if the values are already synchronized and updates the value in User Settings since they are not. This then triggers the paired Flow which checks if the values are synchronized, sees that they are, and exits without making an update.

 

To implement this logic, we first need to add a Get a Record action so we have the User Settings value for comparison. Put this step between the trigger and the update  action. In this example we choose the (Current) environment, the User Settings entity and use the trigger’s User lookup field as the record identifier.

Now that we have both of the timezone values we want to stay synchronized, we can compare them directly with a Condition step.

The final step is to add a Terminate action in the If Yes result of the condition. Make sure to set the Status to Cancelled or Succeeded so you can distinguish this result from actual errors.

Alternatively, the same result can be achieved by moving the Update a Record step into the If no result to prevent it from being triggered if the Flows are in a loop.

The Flow will now only execute the Update action if the values aren’t already in sync, thereby breaking infinite loops. In practice this means the first Flow runs normally and triggers the second Flow, which terminates on the check, breaking the loop.

Your final Flow should look like this:

I recommend adding the infinite loop check to the other Flow in the pair, but as long as one Flow in the loop has a check condition like this, then the infinite loop will be avoided.

This strategy can be expanded to more complicated update steps by adding additional And rows to the check condition, using variables to calculate what a complex field should be, and more.

Sam is a Senior Consultant in RSM's Dynamics practice. He has experience working with many aspects of Dynamics CE as well as integration technologies like Scribe and SmartConnect.

Receive Posts by Email

Subscribe and receive notifications of new posts by email.