Problem: I have a requirement to loop through a collection until it satisfies condition and do some operation.
As always 🙂 requirement sounds simple (especially if you are from programming background) where we can achieve using For/Foreach or While loops.
But Can you believe that in powerapps we only have ForAll (so far) which loops through the entire list/collection without stopping for any condition?
Initially I thought to use ForAll and try to tweak by adding some condition but no luck. when am further exploring options i thought we can use Timer control. YES, you heard it right, the Timer control in Canvas apps can be used to do ForEach / While loops within Canvas app.
Let me brief here on how timer control works (for completeness of the post)
Timer control – A control that can determine how your app responds after a certain amount of time passes.
Key Properties we use for Looping
AutoStart – Whether the timer control automatically starts to play when the user navigates to the screen that contains that control.
Duration – How long a timer runs in milliseconds. There is no maximum value.
OnTimerEnd – How an app responds when a timer finishes running.
Repeat – Whether a timer automatically restarts when it finishes running.
In my CDS entity, I have hierarchy of records example Site,Building,Floor,Room.
Site record is Parent to Building record, Building record is parent to Floor Record so on..
I can add any number of records to the entity and set hierarchy (means its quite dynamic).
If I select Site record, I want show all child types in a list in my Canvas app (i.e. Site,Building,Floor,Room in this case)
here’s is how I have implemented it.
Add a timer control to the form.
Set AutoStart property to true (because I don’t want to wait to fill my gallery)
Set Duration property to 1000 (depends upon the time it takes you to query the data and fill the collection)
Screen Onvisible property – get the first value add it to the final collection i.e.Site value in this case ClearCollect(colPropertyTypes, Filter(‘Property Types’,’Parent Property Type’.’Property Type’ = [@ModelDrivenFormIntegration].Item.Type.’Property Type’)); — gets my site record
ClearCollect(colFinalPropertyTypes,colPropertyTypes); — adds site record to colFinalPropertyTypes collection
Set OnTimerEnd Property to retrieve next level value (e.g Property type whose parent is Site (we get building record) and next time property Type whose parent is Building (we get Floor record) etc
ClearCollect(colChildPropertyTypes, Filter(‘Property Types’,’Parent Property Type’.’Property Type’ = Last(colFinalPropertyTypes).’Property Type’)); —Retrieves my next level record
Collect(colFinalPropertyTypes,colChildPropertyTypes); — Collecting it in my final Collection
By now My final collection has two records –Site, Building. My Child collection has one record – Building (I am using clearcollect to clear value evertime before its collecting, so it always has one latest child value) – We use this scenario to repeat my timer control.
Set Repeat Property to CountRows(colChildPropertyTypes) > 0. which means if we have another child then it repeats the whole operation to get its child.
Finally My Gallery looks like this.
if there is no further child then Repeat condition returns false so it stops my timer.
Simple isn’t it:-)
If you realize we actually learnt two different things from this post.
1. How to loop through (foreach/while) records in Canvas app?
2. How to retrieve iterative child records from CDS entity?
For any doubts or questions, please contact me on firstname.lastname@example.org