Recently, I came across a Power App gallery control / ForAll gotcha which I managed to handle so I’m sharing this here in case anyone else experiences the same issue.
OK, let’s set the scene: It’s a sunny Thursday afternoon in May in central Scotland. The hero is working on a Power App, and binds the following collection (colItems) to a gallery control:
ClearCollect(colItems,
{
ID:1,
Name:"Dog"
},
{
ID:2,
Name:"Cat"
},
{
ID:3,
Name:"Rabbit"
}
);
Great, the gallery is showing the data collection as expected!
Next up, I click a button which performs the following:
Clear(colTest1);
ForAll(galItems.AllItems,
Collect(colTest1,
{
ID: txtID.Text,
Name: txtName.Text
}
);
);
Cool, the colTest1 collection data looks good.
You know what, I want to use the As operator to alias each record as “locItem” to make future code easier to read when it becomes more complex later on. Let’s do that:
Clear(colTest2);
ForAll(galItems.AllItems As locItem,
Collect(colTest2,
{
ID: txtID.Text,
Name: txtName.Text
}
);
);
Woah, hang on! That’s not what I was expecting - the first record has been repeated!?
OK, let’s try a different approach: we’ll use the With function to alias the record as “locItem” instead:
Clear(colTest3);
ForAll(galItems.AllItems,
With(
{
locItem:ThisRecord
},
Collect(colTest3,
{
ID: txtID.Text,
Name: txtName.Text
}
);
);
);
Yup, that’s now working as expected!
Notes
This issue was experienced in a real-world app and manifested itself further downstream, which was traced back to the gallery control / ForAll code similar to this example.
However, please note that this walkthrough of the issue and solution has been intentionally stripped-back to remove any “noise” and keep it easy to follow along with.
The real-world app had a very niche requirement to access all gallery control items as well as text input control values, and involved extra code.
Hope this helps if you experience something similar!