One of the common requirements in workflow development is to pause the running workflow and wait for an event to resume execution. Though Workflow engines like Nintex have this capability Power Automate does not have this functionality inbuilt.
In this article, we will see how to simulate the Wait for an event action and see how the flow can be woken up to continue execution.
We will be using a combination of the below actions to accomplish the Wait for an event action
- Do Until
- Get Item
Let’s take a sample scenario where we have a custom list with the Columns: Title and Total Sales. We will create a Flow that will run on item creation. So when we created the item with Title January, the flow is invoked and we will add the Wait for Action simulation which will cause the Flow to Kind of Pause Until the user comes back and Update the Total Sales Value. Once the total Sales value is added by the user, we will update the Audited? column to Yes
So we can essentially see here that once the item is created, we are pausing the flow until the Total Sales value is updated so that we can resume the flow execution to update another column with a value.
Let’s create the flow with the trigger action When an Item is created
We will then add the combination of 3 actions Do Until,Delay, and Get Item to simulate the Wait for an event action as shown below :
The Total Sales field used in the Do Until has to come from the Get Item action. The reason is that the Total Sales value from the “When an Item is created” section will always have the same value which was there during the time when the flow was invoked for the first time. However, the Do Until will loop at an interval and we have to get the latest item value of Total Sales so as to check if the value has been changed in between. If it has, then that is a clear indication that the user has made an update and the wait for the event has completed which will cause the loop to exit. We can then proceed with the remaining logic as the event for which we were waiting has been completed now.
However, if we do not put a delay inside the Do Untill, it will complete the looping soon enough and will exit the loop even if the exit condition is not met. To Avoid this, we will do 2 things:
- By default DO UNTIL iterations are 60 times and time out is 1 hr. Lets update the Time out setting for the Do Until loop
It follows the ISO 8601 Standard. PT1H means 1 hour, PT72H means 72 hours time period. Here we can increase the Time Out to a maximum of 30 days. Same way we can increase the Count to a maximum of 5000 .
- So as to add a delay between each iteration, we can include a delay of a specific time so that the do until iteration will execute only after that interval. Thus the loop will check for the item value only at this predefined interval. For testing, I have added a 5-second delay but depending on your production environment and at what time interval you want the loop to execute each iteration, this can be increased.
To test the loop exit, let’s add the update action which will update the Audited field to Yes
Now, let’s test the flow by creating a new item.
The Flow has been running since the item update and after 3+ Minutes I have updated the Total Sales value to 50000. This time the flow exited the loop and went on to completion.
As we can see the loop ran every 5 seconds in the 3-minute time resulting in 38 iterations. As soon as the item was updated with a change in Total sales, the loop exited and updated the Audited? Column to Yes.
Thus we saw how to simulate a wait for event action using Do Until, Delay, and Get Item action.