ForAll function comes in. In this post, we'll break down what ForAll does, how to use it, and the common mistakes beginners make.1. What is ForAll?
ForAll is a function that runs the same formula or set of actions for every record in a table. Unlike most Power Apps functions that simply calculate and return a value, ForAll is mainly used for actions such as updating records with Patch, adding items with Collect, or removing records with Remove.
2. Basic Syntax
ForAll(
Table,
Formula
)
Inside the formula, you can reference the current record using ThisRecord. This is similar to how you use ThisItem inside a gallery.
3. Simple Example: Building a Collection
ForAll(
[1,2,3,4,5],
Collect(
MyCollection,
{
Number: Value,
Square: Value * Value
}
)
)
After running this formula, the collection contains values similar to these:
- Number: 1, Square: 1
- Number: 2, Square: 4
- Number: 3, Square: 9
- Number: 4, Square: 16
- Number: 5, Square: 25
4. Practical Use Case: Approving Selected Requests
Imagine that a gallery displays employee requests. Each request has a checkbox, and the user wants to approve all selected requests by clicking one button.
First, the gallery might contain a checkbox named chkSelect. The button's OnSelect property can use this formula:
ForAll(
Filter(
galRequests.AllItems,
chkSelect.Value = true
),
Patch(
Requests,
ThisRecord,
{
Status: "Approved",
ApprovedDate: Today()
}
)
);
Notify(
"Selected requests have been approved.",
NotificationType.Success
)
Here is what happens:
galRequests.AllItemsgets the records currently displayed in the gallery.Filterkeeps only the records whose checkbox is selected.ForAllprocesses each selected request.Patchupdates its status and approval date.Notifydisplays a confirmation message.
5. Practical Use Case: Updating Pending Orders
Suppose an orders data source contains several orders with a status of Pending. You want to mark all pending orders as processed.
ClearCollect(
colPendingRequests,
Filter(
Orders,
Status = "Pending"
));
ForAll(
colPendingRequests,
Patch(
Orders,
ThisRecord,
{
Status: "Processed"
}
)
)
This formula filters the orders first and then updates every matching record. The use of Filter is important because it prevents unrelated records from being changed.
6. Practical Use Case: Creating Invoice Lines
Assume that a shopping cart is stored in a collection named CartItems. When the user submits an order, you may need to create an invoice line for every item in the cart.
ForAll(
CartItems,
Patch(
InvoiceLines,
Defaults(InvoiceLines),
{
InvoiceNumber: varInvoiceNumber,
ProductName: ThisRecord.ProductName,
Quantity: ThisRecord.Quantity,
UnitPrice: ThisRecord.UnitPrice,
LineTotal: ThisRecord.Quantity * ThisRecord.UnitPrice
}
)
)
For each item in CartItems, Power Apps creates a new record in the InvoiceLines data source.
The expression below calculates the total for each invoice line:
ThisRecord.Quantity * ThisRecord.UnitPrice
7. Practical Use Case: Copying Records into a Collection
You may want to copy selected records from a gallery into a temporary collection for later processing.
ClearCollect(
SelectedProducts,
Filter(
galProducts.AllItems,
chkProduct.Value = true
)
)
In this case, ClearCollect is usually simpler than ForAll because the entire filtered table can be collected at once.
However, if you need to create a different structure for every selected record, ForAll can be useful:
Clear(MyProductSummary);
ForAll(
Filter(
galProducts.AllItems,
chkProduct.Value = true
),
Collect(
MyProductSummary,
{
Product: ThisRecord.ProductName,
ExtendedPrice: ThisRecord.Quantity * ThisRecord.Price
}
)
)
9. Practical Use Case: Calculating Employee Bonuses
Suppose an employee collection contains EmployeeName and Salary fields. You want to calculate a bonus equal to 10 percent of each employee's salary.
For a local collection, you can create a new collection containing the calculated bonus:
ClearCollect(
EmployeesWithBonus,
ForAll(
Employees,
{
EmployeeName: ThisRecord.EmployeeName,
Salary: ThisRecord.Salary,
Bonus: ThisRecord.Salary * 0.1
}
)
)
This formula does not change the original Employees collection. Instead, it creates a new collection named EmployeesWithBonus.
10. Practical Use Case: Removing Selected Records
ForAll can be used to remove multiple selected records. For example, a user can select products in a gallery and click a Delete button.
ForAll(
Filter(
galProducts.AllItems,
chkProduct.Value = true
),
Remove(
Products,
ThisRecord
)
);
Notify(
"Selected products have been deleted.",
NotificationType.Success
)
Always filter the records carefully before using a bulk delete formula. A mistake in the filter could remove more records than intended.
11. Practical Use Case: Adding Multiple Items to a SharePoint List
Suppose a user enters several tasks into a local collection named TaskCollection. You can create a SharePoint record for each task when the user clicks Submit.
ForAll(
TaskCollection,
Patch(
ProjectTasks,
Defaults(ProjectTasks),
{
Title: ThisRecord.TaskName,
Priority: ThisRecord.Priority,
AssignedTo: ThisRecord.AssignedTo,
DueDate: ThisRecord.DueDate
}
)
);
Notify(
"All tasks have been submitted.",
NotificationType.Success
)
Here, Defaults(ProjectTasks) tells Power Apps to create a new record instead of updating an existing one.
12. ForAll Actually Returns a Table
Even though ForAll is often used for actions, it can also return a table containing the result from each iteration.
ForAll(
[1, 2, 3],
Value * 2
)
This produces a table containing values similar to:
- 2
- 4
- 6
Another example creates a table with named columns:
ForAll(
Sequence(3),
{
Number: Value,
DoubleNumber: Value * 2
}
)
13. Common Pitfalls to Avoid
- Do not rely on
ForAllto process records in a strict sequential order. - Avoid nesting one
ForAllfunction inside another unless it is necessary. ForAlldoes not clear existing data. UseClearorClearCollectwhen appropriate.- Be careful when using
ForAllwith large data sources because delegation limitations may apply. - When creating new records, use
Defaults(DataSource)withPatch. - When updating an existing record, make sure that the record supplied to
Patchis the correct record.
14. Performance Tips
Do not use ForAll for simple calculations when a built-in function can perform the task more efficiently.
For example, use Sum to calculate a total:
Sum(
CartItems,
Quantity * UnitPrice
)
Use CountRows to count records:
CountRows(
Filter(
Orders,
Status = "Pending"
)
)
Use ForAll when you need to perform a separate action for each record, such as creating, updating, or deleting records.
15. Try It Yourself
Create a collection of employees with the following columns:
EmployeeNameSalaryBonus
Then create a new collection that calculates a bonus equal to 10 percent of the salary:
ClearCollect(
EmployeesWithBonus,
ForAll(
Employees,
{
EmployeeName: ThisRecord.EmployeeName,
Salary: ThisRecord.Salary,
Bonus: ThisRecord.Salary * 0.1
}
)
)
Next, try modifying the example so that only employees with a salary greater than 50,000 receive the calculated bonus.
ClearCollect(
EmployeesWithBonus,
ForAll(
Filter(
Employees,
Salary > 50000
),
{
EmployeeName: ThisRecord.EmployeeName,
Salary: ThisRecord.Salary,
Bonus: ThisRecord.Salary * 0.1
}
)
)
16. Wrap-Up
ForAll is one of the most useful functions in Power Apps Canvas apps when you need to perform an action on each record in a table or collection.
Common uses include:
- Updating multiple records with
Patch - Creating multiple records with
DefaultsandPatch - Adding transformed records to a collection with
Collect - Removing multiple records with
Remove - Processing selected records from a gallery
Use ForAll when each record requires an individual action. For simple totals, counts, and calculations, use functions such as Sum, CountRows, and Filter whenever possible.
Quick Recap Questions:
- What does
ForAlldo? - How do you refer to the current record inside
ForAll? - How can you update multiple records with
Patch? - What is the purpose of
Defaults(DataSource)? - When should you use
SumorCountRowsinstead ofForAll?

0 Comments