Skip to content
Home » ForAll function in Power Apps

ForAll function in Power Apps

The ForAll function in Power Apps is used to perform a specific action or apply a formula to each record in a table or collection. It allows you to iterates over a set of data and execute an action for each item in that set, such as updating records, creating new ones, or performing calculations.




If the formula returns a record, the resulting table will include records with the same columns as those in the returned record.

If the formula returns a single value, the resulting table will be a single-column table. However, if the formula returns a record, the resulting table will include records with the same columns as the returned record.

If the result of the formula is a blank value, then there is no record in the result table for that input record.

Syntax:

ForAll(Table, Formula)
  • Table: This is the table or collection of records you want to iterate through.
  • Formula: The action or formula to be applied to each record.

When to use the ForAll:

  1. Conditional Actions: Perform actions on each record based on a condition.
  2. Bulk Updates: You can update multiple records at once instead of writing individual update statements.
  3. Bulk Insertions: You can insert multiple records into a data source in one go.

Here, we have a gallery called gal_emp that displays employee details. The gallery is connected to a SharePoint list called ‘Employee Info’.

Using ForAll function to update the mulitple records

Example 1: ForAll with Patch Function

Suppose you want to update the location to ‘Noida’ for all employees shown in the gallery. You can use the ForAll function along with Patch to update each record.

Add a Button control and named it ‘Update’, then go to OnSelect property of button and write a below formula.

ForAll(
    gal_emp.AllItems,
    Patch(
        'Employee Info',
        ThisRecord,
        {'Office Location': "Noida"}
    )
)

Lets understand the above formula:

  • ForAll: Loops through all items in the gal_emp gallery.
  • Patch: Updates records in the ‘Employee Info’ SharePoint list.
  • LookUp: Finds the corresponding record in Employee Info by matching the ID from the gallery items.
  • ThisRecord: Refers to the current record in the gallery being processed.

Now, you will see when you click on Update button. It updates the office location to “Noida” for all the records in the gallery.

Example2: Updating records based on conditions

Suppose, you want update specific records based on certain conditions.




Let’s modify the formula to update the Level to “L4” for all records in the gallery belonging to the “HR” department.

Go to the OnSelect property of Update button and write the formula.

ForAll(
    Filter(
        gal_emp.AllItems,
        Department = "HR"
    ),
    Patch(
        'Employee Info',
        ThisRecord,
        {Level: "L4"}
    )
)
Lets understand the formula:
  • The Filter function retrieves all items from gal_emp.AllItems where the Department is “HR”.
  • The ForAll function iterates through each of the filtered records.
  • For each record that meets the filter criteria, the Patch function updates the corresponding record in the ‘Employee Info’ data source, setting the Level to “L4”.
Let’s run the app and click the Update button to see the results. You will see that it updates the Level of records belonging to the “HR” department to L4.

Example3: Updating records based on multiple conditions 

Let’s add update the office location “Delhi” for all records belongs to “HR” department otherwise “Gurgaon”.

Go to the OnSelect property of Update button and write the below formula.

ForAll(
    gal_emp.AllItems,
    If(
        Department = "HR",
        Patch(
           'Employee Info',
            ThisRecord,
            {'Office Location': "Delhi"}
        ),
        Patch(
            'Employee Info',
            ThisRecord,
            {'Office Location': "Gurgaon"}
       )
    )
)

 

Let’s run the app and click on the Update the button. You will notice that the office location has been updated to “Delhi” for all records belonging to the “HR” department, while the location for the remaining records has been updated to “Gurgaon”.

 

 key points to keep in mind when using the ForAll function in Power Apps:

  1. Delegation Limitations: The ForAll function is non-delegable, which means that when working with large data sources like SharePoint or SQL, only a limited number of records (by default, up to 2,000) can be processed. If you are dealing with large datasets, you’ll need to manage them in a different way.
  2.  ForAll loops over each item in a table, which can slow down app performance if you use it on large tables or perform actions that require multiple server calls (such as updating records one by one).

 

Recommended for you

Reset multiple control values and all the controls within a form

Submit multiple records to a data source

Loading

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SQL BI Tutorials

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

Continue reading