Power Apps – Using Variables in ForAll()

The ForAll command is very handy if you want to loop around a collection and perform some calculations.  One limiting feature is that you can’t use variables in the ForAll loop, using UpdateContext or Set.

There is a workaround, which is to create and use a single record collection to hold the values which can then be used as variables. The downside is that it does make the “code” not as straightforward to read.

This is the process I use:

Add a line to the Form OnVisible to reset the collection (unless you’re using the variable like a Global in which case put it in the App OnStart).

ClearCollect(colVariables, {colvarVariableOne:””});

To read the variable value use

First(colVariables).colvarVariableOne

To set the variable value use:

Patch(colVariables, First(colVariables), {colvarVariableOne:”NEW VALUE”});

One thing that can catch you out is that collection column types are set when the collection is initiated so set the default value to be of that type. For example to create a datetime column use:

ClearCollect(colVariables, {colvarHoliday:DateTimeValue(“01/01/2000 00:00”});

Photo by Tine Ivanič on Unsplash

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s