How to set a variable inside ForAll in PowerApps Canvas apps

Follow Debajit’s Power Apps & Dynamics 365 Blog on WordPress.com

This blog is again quite simple one and yet quite interesting. In this blog I will discuss about how we can set a variable inside For loop in canvas app.

You might be wondering – “Isn’t UpdateContext and Set function already there in PowerApps to set a variable?”. Yes it is there. But unfortunately they don’t work inside the ForAll function.

If you try to use the function inside ForAll function, you will get the below error.

You will get the same error if you use UpdateContext.

But why you may need to set a variable inside ForAll statement. There can be N number of scenarios but let’s try a very simple one. I have collection like the below one.

Collect(Categories, {'Category': "x"}, {'Category': "y"}, {'Category': "z"}, {'Category': "a"})

And I have a textbox and button. When I click on the button, I will iterate through the Categories Collection and then calculate a score. However the score shall be based on the below formula.

  • If Category “x”, count will be incremented by 2
  • If Category “y”, count will be incremented by 3
  • If Category “z”, count will be incremented by 4
  • If Category “a”, count will be incremented by 5

Would have been quite easy, if I could set a variable inside the ForAll and depending on the category I increment the variable. And then use the variable in the Text property of the Textbox.

Is there no workaround? Off course there is and in-fact using this technique you can accomplish most of the scenarios. Let’s see how we can achieve our requirement with the same workaround.

OnSelect of the button, I use the below formula.

Clear(CountCollection);
ForAll(Categories, Collect(CountCollection, 
Switch(
    ThisRecord.Category, "x", Last(CountCollection).Value + 2,
    "y", Last(CountCollection).Value + 3,
    "z", Last(CountCollection).Value + 4,
    "a", Last(CountCollection).Value + 5
)))

What am I doing in this formula? I have declared a CountCollection. I am first clearing the collection and then performing a switch case inside the ForAll. But the most important part is where I am taking the Last value of the collection and incrementing it as per our logic. The last item stores the latest count till last iteration.

Cool isn’t it? Having done that, I set the Default property of the TextInput control to the below formula.

Text(Last(CountCollection).Value)

As per our logic, if we take the last item in the collection, that stores the latest count. And finally when I click on the button, I get the count as 14 which is the correct count (2 + 3 + 4 + 5).

Hope this helps you till the time Microsoft comes up with support for this feature.

You may also like the below posts.

Debajit Dutta
Business Solutions MVP


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

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

Continue reading