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?
Analysis:
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.
Solution:
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.
Visible – Whether a control appears or is hidden.
So the requirement I have is as follows:
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.
ClearCollect(colFinalPropertyTypes,colPropertyTypes); — adds site record to colFinalPropertyTypes collection
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.
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?
Contact
For any doubts or questions, please contact me on pavankumar88.garlapati@gmail.com
Happy CRMing:-)