Loop Processing in a Canvas App
Looping in a Canvas App?
A little while back I was faced with a scenario where in a Canvas App I had to process an action multiple times, not knowing ahead of tie when to stop this series of repetitions.
Fairly standard code structure for a maker like me who has a developer background, right?
The "Dev" concept being as follow:
or in a more human readable way:
So, I've looked up in the documentation and forums for a statement/function that would be similar to while() or foreach().
The only potential candidates I've found were ForAll() and With() combinations.
But these are more about evaluating records (or a specific one) within a collection, this doesn't address my scenario.
More about the loop scenario
Wanting to have an app where I would review and take notes about the subscribers from my YouTube channel, I created a custom connector to the YouTube API. This API has limitations to return a maximum number of records per call. So for example, let's say I have 250 subscribers , if I'd call this API I would only get the first 50 on the first call and an indicator that there's more records.
Here's a view on the overall process:
When the API is called to retrieve a list of subscribers we can provide a "start page" token, this way it will return the items in the list from the starting point that page represents (about where the last record of the initial page was located). So, the idea here is to call the API for as long as it gives me back a "next page" token which is passed along in the next call to that API as the "start page".
Not too dizy I hope 😉
Canvas App Loop
So, how can we resolve this, not knowing ahead of time how many times we will need to call that YouTube connector in a Canvas App which doesn't have a loop function?
Ingredients (Components):
- tglLoopIndicator : toggle control that will execute the script every time it gets toggled ON
- flgStartLoop : boolean variable on which the toggle control is turned on/off
- strNextLoop : string variable - token for next page received from the API and passed along on the next call
- cltListSubscribers : collection where the retrieved items are stored.. this is specific to this scenario
Recipe (Code & Properties):
Toggle Control tglLoopIndicator
- Default property set to the flgStartLoop
- OnCheck property/event do the call to the API and processing
To launch this, simply set the flgStartLoop to true and it will start. This could be at the OnVisible of a screen, OnStart of the app or at the OnSelect of a button, all depends on your specific scenario.
Other Loop Option: so, this is my way to address a loop scenario. Have a look at a different approach by reading @PavanKumar blogpost: How to do ForEach or Do while loops in power apps (canvas)?
Sample Loop Demo App: if you want to view a sample app and test out how to implement this in your use case, visit the gallery I've uploaded a demo and a YouTube video walkthrough for it.
References used in this Blog:
Custom Connector YouTube API : https://powerdiver.blogspot.com/2019/07/youtube-subscribers-customer-connector.html
YouTube Canvas App: https://powerdiver.blogspot.com/2019/07/youtube-subscribers-canvas-app-deep-dive.html
How to reach me:
LinkedIn: https://www.linkedin.com/in/zepowerdiver
Twitter: https://twitter.com/ZePowerDiver
Facebook: https://www.facebook.com/ZePowerDiver/
YouTube: https://www.youtube.com/channel/UCBEIW1Pzg2dAJD1YukaE2pw
Blogger: https://powerdiver.blogspot.com/
*This post is locked for comments