Canvas Apps: Data Source Polling

I don’t consider myself a Power Platform savant, but I will say this: it’s a lot of fun to take the best components of the Power Platform and craft a solution that works for customers. My primary responsibility is to recommend the best possible solution using the most suitable tools available. What does that mean in practice? For instance, I wouldn’t try to build a reporting canvas app with complex PowerFx patterns. Instead, I would create the reports in Power BI and embed them in the app if needed.

The same concept applies to the integration between Power Automate and Power Apps. Why struggle with complex patterns and many lines of PowerFx when you can leverage Power Automate to handle backend processing? Power Automate is designed to handle these tasks efficiently.

But what if you need feedback from Power Automate within your app? If your processing takes less than two minutes, you’re in good shape. But those pesky edge cases do exist, and there will be times when processing might take a bit longer. That’s when a bit of ingenuity and creativity come into play.

Polling

Have you heard of polling? It’s a process where we repeatedly check a database to see if a specific condition, we’re looking for has been met. In a canvas app, we can implement this by using a timer. When the timer finishes, the app checks Dataverse, SharePoint, SQL, or another data source to see if the condition has been satisfied. If it has, we provide feedback directly to the app. If it hasn’t, the polling continues until the condition is met or until a counter mechanism or variable that stops the polling after a set number of attempts.

Step 1: Setup Timer

In our canvas app we want to add the timer control and add the following parameters to the each respective property listed below:

PropertyValueDescription
Duration6000Timer duration will be 6 seconds
OnTimerEndIf(
IsBlank(
LookUp(
colTestTimer,
Id = 0
).Column1
),
UpdateContext({ctxEmptyCol: true}),
UpdateContext({ctxEmptyCol: false})
)
If Column1 in the colTestTimer collection is empty, set the context variable ctxEmptyCol to true; otherwise, set it to false. If the context variable is false, then the timer will stop.
RepeattrueThe timer will continue to repeat until the polling results are satisfactory.
Reset!ctxEmptyColReset the timer once all conditions are fulfilled.
StartctxEmptyColInitiate the timer as Column1 in the collection is empty.

Step 2: Test

To test our timer, we will require two buttons: one to create the collection with Column1 being empty, and another to initiate a write to Column1 and stop the polling.

Button 1

In the OnSelect property of the first button, paste the following code:

ClearCollect(colTestTimer, {Column1: Text(Blank()), Id: 0}); UpdateContext({ctxEmptyCol: true})

Button 2

In the OnSelect property of the second button, paste the following code:

UpdateIf(colTestTimer, Id = 0, {Column1: "Not Empty"})

Test

Launch the application. Press the first button to initiate the timer and fill the collection. Allow the timer to complete several cycles, and once you’re prepared, hit the second button to refresh the collection. Upon the timer’s completion, it will check the collection, recognize that the condition has been met, and cease polling.

Conclusion

In this article, the focus is on polling a collection; however, your options are not confined to this collection alone. It’s crucial to use polling judiciously and construct patterns that do not impede your app’s performance. An example of this is polling a table to check if a file has been created and linked to a record. Undoubtedly, you can discover numerous intriguing use cases to further this concept.

Leave a Reply

Discover more from Duke DeVan

Subscribe now to keep reading and get access to the full archive.

Continue reading