Skip to main content

PowerApps – Running Functions in Parallel using Timers

In a previous post I used PowerApps to query Dynamics to check if a list of email addresses exists in my instance as contacts. I used the ForAll function to lookup Dynamics for each email in my collection sequentially. This worked  fine until I had to check if 500  emails exist in  my instance containing just over 300,000 contacts.

After doing some performance bench-marking ( Beginning the search and starting a stop watch on my phone) this query took 12 mins 53 seconds to run. Would be great if multiple lookups could run in parallel. I used timers to run the exact same ForAll statements three extra times , similar to separate threads which greatly improved the speed. Although I had to make sure each email wasn’t processed multiple times.

My original statement was:

ForAll(SearchEmails,Collect(Matches,LookUp(Contacts,emailaddress1 = Result))))

SearchEmails has a column called Results which contains the emails I want to find. For every email in SearchEmails , check if it matches the email address 1 field of a contact record in Dynamics. If one is found add it to a collection called Matches.

ClearCollect(ProcessedEmails,{email:” “});

I created a collection ,ProcessedEmails, this will store emails that have already been searched for.

Edited the original ForAll statement to only lookup emails that do not exist in the processed emails collection. Before the email is looked up, it is added to the processed list so another thread/Timer doesn’t pick it up but moves onto the next email.

ForAll(SearchEmails,If(!(Result in, Collect(ProcessedEmails,{email:Result}); Collect(Matches,LookUp(Contacts,emailaddress1 = Result))))

The above ForAll runs  on visible of my search page, I also then created  3 timer controls  each with the same for all statement in their on timer end functions.

Timer Properties

Auto-start : true , Repeat = false.


Timer 1 4000

Timer 2 8000

Timer 3  12000

When the search page is visible,

  • The original search will run on the OnVisible function
  • Timer 1 will start searching  four seconds later
  • Timer 2 four seconds after timer 1
  • Timer 3 four  seconds after  timer 2

This was to stop all my timers starting the search at the exact same time and processing the same emails.

With the For All lookup running 4 times the search went down from 12:53 mins to 3:21

Im sure additional timers could be added to possibly improve performance even further

Full formulas used

Search Page on visible

(can be applied to a button instead but make sure you start the timers)

Clear(Matches);ClearCollect(ProcessedEmails,{email:”something”}); Set(vSearching,true);ForAll(SearchEmails,If(!(Result in, Collect(ProcessedEmails,{email:Result}); Collect(Matches,LookUp(Contacts,emailaddress1 = Result)))); Set(vSearching,false)

Timers OnTimerEnd

ForAll(SearchEmails,If(!(Result in, Collect(ProcessedEmails,{email:Result}); Collect(Matches,LookUp(Contacts,emailaddress1 = Result))))


Charles Osei

Dynamics 365 Consultant @ Hitachi MCSE Business Applications Computer Science – University of Lincoln 2017 Nottingham D365/CRM user group Chapter Leader TDG – Community leader Twitter @charlied3v LinedIn

Leave a Reply