In this tutorial, you’ll learn how to use the powerful For All function in Microsoft Power Apps to create loops, iterate through records, and execute actions on each record.
By the end, you’ll know how to generate collections, apply discounts, filter datasets, and even update your data source using ForAll.
Understanding the Scenario
Imagine you have a Dataverse table called Products that includes product names, prices, and an initially empty Discounted Price column. Your goal is to loop through this table and calculate a discounted price for each product.
Generating a Collection Using ForAll
Begin by creating a collection from the Products table:
ClearCollect(
updatedProducts,
ForAll(
Products,
{
name: ThisRecord.Name,
price: ThisRecord.Price,
discountedPrice: ThisRecord.DiscountedPrice
}
)
)
This will generate a new collection named updatedProducts that contains all records from the Products table.
Displaying the Collection in a Gallery
To visualize the collection, insert a vertical gallery and set its Items property to updatedProducts. You can customize the layout to show fields like Name, Price, and Discounted Price.
Using Sequence in ForAll
You can also use the Sequence function to loop through a specific number of records. For example:
ClearCollect(
updatedProducts2,
ForAll(
Sequence(4),
{
name: Index(Products, Value).Name,
price: Index(Products, Value).Price,
discountedPrice: Index(Products, Value).DiscountedPrice
}
)
)
This will create a collection with only the first four records from the Products table.
Applying Discounts Using Patch and ForAll
You can apply a discount to every item in the collection like this:
ForAll(
updatedProducts,
Patch(
updatedProducts,
ThisRecord,
{
discountedPrice: price * 0.9
}
)
)
This applies a 10% discount to all records in the updatedProducts collection.
Using Filter Inside ForAll
You can filter the records and apply a different logic to a subset. For example, apply a 15% discount only to products priced above 500:
ForAll(
Filter(updatedProducts, price > 500),
Patch(
updatedProducts,
ThisRecord,
{
discountedPrice: price * 0.85
}
)
)
Saving Data Back to the Data Source
So far, changes have been made only in memory. To write the changes back to the Dataverse Products table:
ForAll(
updatedProducts,
Patch(
Products,
LookUp(Products, name = ThisRecord.name),
{
discountedPrice: discountedPrice
}
)
);
Notify("Update successful", NotificationType.Success);
This code uses Patch to update each record in the Products table by matching the product name and updating the discounted price.
Conclusion
The ForAll function in Power Apps is an incredibly powerful tool for processing data row-by-row. Whether you’re creating collections, filtering datasets, applying business rules, or updating the data source, mastering ForAll opens the door to advanced app logic and automation.
Experiment with these patterns in your own apps and combine them with functions like Patch, Filter, and Sequence to build dynamic and responsive solutions.

Leave a Comment