Categories
Skip Navigation Links

PowerApps - ForAll function with examples

In this article, we are going to go through various ForAll function examples that can be used in your applications. ForAll is one of the widely used functions in PowerApps. Let’s understand in detail, how and where you can use the ForAll function using some examples.

Note:

  • In PowerApps, when you want to execute some functionality on each row on a specific table/database/collection, you can use ForAll function
  • The function inside the ForAll, cannot operate on the same data source that is used in ForAll, we will check this in detail in the Examples section below.
  • Input and return value: Whatever is the input of the ForAll function, the same will be the return value as well. So, if you use a table in For Loop, the return value will be a table only. Same way, you can use a collection or single-column table also.
  • Records can be processed in any order.
  • You can perform multiple operations by using separator (;).
  • You CANNOT use a function like Upatecontext, Clear, ClearCollect, Set, etc… because these functions can hold the variable which is vulnerable to the ordering issues for the current For loop.

PowerApps ForAll function Syntax

ForAll(Table, Formula)

  • Table: This is required. Pass the table or collection or data source to process.
  • Formula: This is required.
    • This will be executed for each row of the table passed in the first parameter.

PowerApps ForAll Functions Examples

We will use the below table for our examples.

Table Name: Weathers

PowerApps Examples

The function inside the ForAll, cannot operate on the same data source that is used in ForAll

ForAll cannot operate on the same data source
  • Look at the script. The data source used in ForAll and the data source used inside Forall (Patch function) are the same. This is NOT CORRECT.
  • As the statement specifies, you cannot just loop through the same data and simultaneously update it also.
  • Solution:
    • The simple solution about it is you can create a collection using actual data and loop through that collection.
    • Call this formula: ClearCollect(TempCollection, Weathers);
    • Now you can work on TemCollection.

ForAll(TempCollection, Patch(

        Weathers,

        First(Filter(Weathers, Humidity > 75 ) ),

        {Temperature: 97}

    )

)

The right side of the “Equal” operator must be a constant value

right side of the Equal operator must be a constant
  • Issue:
    • The above-mentioned error we get when we try to loop through the items but in the same condition, we have used ThisRecord.ColumnName for comparison in the formula
    • Here, we have created a collection(TempCollection) using a table(Weathers).
    • Looping through each record of collection, we are patching the record in the table which matches the formula.
    • Now, in the formula, we have used ThisRecord.Columnname – which takes column from the collection (which is in Loop right now)
    • BUT, PowerApps is denying this operation saying on the right-hand side you can specify only a static value to compare.
  • Solution:
    • While creating a collection (TempCollection), we will rename the column that we want to use in the ForAll function.
    • Then we can use that renamed column in For loop without any error.
    • Here, you can notice that even though we haven’t used ThisRecord.ColumnName in condition, we have used it while updating the value back in the database.
      ClearCollect(TempCollection, RenameColumns(Weathers, "humidity", "newHumidity"));

      ForAll(TempCollection,

          Patch(

              Weathers,

              LookUp(Weathers,Humidity = newHumidity),                

              {Temperature: Value(ThisRecord.Temperature) + 100}

          )

      )

PowerApps ForAll Patch current item using Result

  • If you have a single column collection, then you can use Result to get the current item in the loop.

In this example, we will get a distinct record from a table and create a collection, and then based on that collection we will run a For Loop.

  • You can Patch multiple records using below PowerApps formula.
  • NOTE: This is a very important formula to keep not of as it contains widely used functions of PowerApps - ForAll, Patch, and LookUp.


    ClearCollect(

        TempCollection,

        Distinct(

            Weathers,

            Humidity

        )

    );



    ForAll(

        TempCollection,

        Patch(

            Weathers,

            LookUp(

                Weathers,

                Humidity = Result

            ),

            {Temperature: Value(ThisRecord.Temperature) + 100}

        )

    );

  • Explanation: In the first part of the example, we are creating a collection with a distinct value of the Humidity column.
  • In the second part, we are using Result in the formula which is nothing but the Humidity column we added in the earlier step.

PowerApps ForAll Split

  • If you want to split a string and loop through the result of it then you can use the below formula.
  • PowerApps ForAll Split
  • Use the Split formula in ForAll function’s Source parameter.
  • You can grab the table of substrings (Output of Split) into a collection using Result in ForAll function.
  • We are collecting substrings in FinalCollectionUsingSplit collection

    ForAll(

        Split("Lorem Ipsum is simply dummy text of the printing and typesetting industry", " "),

        Collect(

            FinalCollectionUsingSplit,

            Result

        )

    );

PowerApps ForAll RemoveIf

PowerApps ForAll RemoveIf
  • If you want to delete records while looping through the records, you can use the below formula.

    ForAll(

        TempCollection, RemoveIf(Weathers, newHumidity = Value(ThisRecord.Humidity) + 10 )   

    );

PowerApps ForAll Patch defaults

PowerApps ForAll Patch defaults
  • In Patch, Default is used when you want to create a new record in your table.
  • When you want to create a new record in your table for each row that you are looping in your ForAll function, you can use this formula.

    ForAll(

        TempCollection,

        Patch(Weathers, Defaults(Weathers), {Temperature: ThisRecord.Temperature});

    );

PowerApps ForAll get current record

  • If there are multiple columns in the collection being operated for ForAll, you can get the current records by ThisRecords.ColumnName

    PowerApps ForAll get current record
  • If there is only one column in the collection being operated for ForAll, you can get the current record by result
    PowerApps ForAll get current result

PowerApps ForAll UpdateIf function

PowerApps ForAll UpdateIf function
  • UpdateIf has a similar implementation like Patch in the ForAll
  • The only difference is how UpdateIf performs on data.
  • Patch updates or creates only one row at a time while UpdateIf can update more than one record at a time.

Let us know if you have any difficulties using the ForAll function, we will try to cover them here.


We value your Feedback:

Page URL:

Name:

 

Email:

 
 

Suggestion:

 

© 2020 Code SharePoint