Wednesday, August 21, 2024

PowerAutomate Flows Trigger Multiple Times - a proposed workaround

Every now and then, I get a PowerAutomate Flow that seems to trigger more than once, and most of the time it is not a problem, but sometimes they are running in parallel and end up creating duplicate records which is undesirable. It happens most often when a Dataverse trigger Change Type is "Added or Modified". I suspect that something in the API is updating the record right after creating it. 

There is a "Concurrency Control" feature under the trigger settings that allows you to control running the flow in parallel. If you set this to 1, and save the flow, you cannot turn it off, so consider your options first. What this feature does is make your flows run one after the other, rather than at the same time (in parallel). While it can solve some of the problems with flows running in parallel, I have had situations where it did not. 

Recently I needed to generate a single record in an unrelated table, but the flow was running twice in parallel so two records were created. I came up with a workaround to get around this issue. Create a "Token Gate" field on the trigger table (single line of text) and follow these steps:
  1. When the flow starts, initialize a string variable called "Token" with a "guid()" expression
  2. Update the triggering record's Token Gate field using the Token from step #1
  3. Delay a few seconds (or up to 1 minute) to allow any parallel flows to complete step #2
  4. Get the trigger record (the one you updated in step #2)
  5. If the token generated in step #1 is not equal to the token received in step #4, then terminate the flow. 
Be sure that the trigger action uses Select Columns or Trigger Conditions so that step #2 does not trigger an endless loop of flows.