As far as we know, Power Apps have various functions that make applications more attractive and responsive. One of the most useful controls among them is the Power Apps ForAll function.
In this Power Apps tutorial, I will explore what is the forall in PowerApps and its syntax; then, we will see how to use the ForAll function in Power Apps. Also, by taking some simple scenarios, We will cover all these below topics:
- How to use Power Apps ForAll Collection
- Working with Power Apps ForAll Set Variable
- What is Power Apps ForAll Split
- Power Apps ForAll Patch or Power Apps ForAll Patch SharePoint List
- Power Apps ForAll Current Item
forall PowerApps
Power Apps ForAll() helps evaluate the formula and perform actions for all the records in a table. It evaluates some functionality on each row of a particular table/collection or database.
In the Power Apps ForAll function, the input and return values are the same. That means whatever you apply to the input value will return the same. If the return value is a bank value, there is no record in the result table. There may be fewer records in the result table than in the source table.
In the ForAll function in Power Apps, you can perform multiple operations using a separator (;). ForAll is not delegable while using the other functions, such as a Filter.
Note:
One important thing you should know is that some Power Apps functions like UpdateContext, ClearCollect, Clear, Remove, Set, etc., can hold the variable, which is vulnerable to the ordering issues for the for loop. So, we can not use these types of functions
Syntax of the Power Apps ForAll Function:
ForAll(Table, Formula)
Where,
- Table = This is required to specify the table to be acted upon
- Formula = This is also required that specifies the formula to evaluate all records of the Table
Power Apps ForAll Function Examples
In this example, we will see a simple calculation using the Squares data source.
Input:
To create the Data source as a Power Apps Collection, you can use the below formula on Apps’s OnStart property.
OnStart = ClearCollect(
Squares,
[
"1",
"4",
"9"
]
Insert a List box control on the Power Apps Screen and set its Items property to the code below.
Items = ForAll(
Squares,
Sqrt(Value)
)
OR
Sqrt( Squares )
Where,
- Squares = Power Apps collection name
Output:
For all the records of the input table, raise the Value column to the third power. The Power function does not support single-column tables. Therefore, the ForAll function must be used in this case.
To do so, follow the below code.
Items = ForAll(
Squares,
Power(
Value,
2
)
)
Power Apps ForAll Collection
In this example, I have used my collection, “Statements,” which has single-column table values.
To create the Power Apps Collection, you can use the below formula on Apps’s OnStart property.
Onstart = ClearCollect(
Statements,
[
"Goodmorning",
"Goodbye",
"Hello"
]
)
Now, on the Power Apps Screen, insert a Data table control and set its Items property to the code below.
Items = ForAll(
Statements,
MicrosoftTranslator.Translate(
Value,
"fr"
)
)
Where,
- Statements = Power Apps collection name
- MicrosoftTranslator = We can use this data connection to translate the values
- “fr” = You can translate the contents of the Value column into French (abbreviated “fr”)
To display the data table field value, click on the Edit fields, select the + Add field option to choose a field, and select the Add button.
Finally, have a look at the below screenshot for the output.
This is how we can work with the Power Apps ForAll Collection.
Power Apps ForAll Set Variable
Suppose you want to set a variable within the Power Apps ForAll function. Then, first of all, a simple question that may come to your mind is, Is it possible to set a variable within a ForAll function?
The answer to this question is No. You can not set a variable within a ForAll function, which is not currently supported in Power Apps.
To fix this issue, you can use the Power Apps collection. To do so, follow the below steps.
1. To create a Power Apps variable, apply the below formula on the App’s OnStart property.
OnStart = ClearCollect(
colVariables,
{colvarVariableOne: "PowerApps"}
);
Where,
- ClearCollect = ClearCollect is a method used to clear all the records from the Power Apps Collections and add a new record to them
- colVariables = This is the variable name
- “PowerApps” = This is a string value that I have specified
2. Now, insert a Text Label control and set its Text property as:
Text = First(colVariables).colvarVariableOne
Similarly, suppose you want to set the value; you can add a Button control and set its OnSelect property to the code below.
OnSelect = Patch(
colVariables,
First(colVariables),
{colvarVariableOne: "New Demo"}
)
Finally, Save, Publish, and Preview the app. Once you click the button control, the specified value will appear in the text label control, as shown below.
Power Apps ForAll Split
In this scenario, we will see how to split a text in Power Apps using a ForAll function. To do so, insert a Button control and set its Onselect property to the code below.
OnSelect = ForAll(
Split(
"This is a simple demo in PowerApps",
" "
),
Collect(
collectUsingSplit,
Value
)
)
Where,
- “This is a simple demo in PowerApps” = This is the sentence to split with every space
- collectUsingSplit = Collection name
Once the user clicks the button control, the result will be stored in the Power Apps collection, as shown below.
Output:
Note:
Not only can you split the string by a space (” “), but you can also split by using a delimiter like Comma (,), Semicolons (;) etc.
This way, we can work with the Power Apps ForAll Split.
Power Apps ForAll Patch | Power Apps ForAll Patch SharePoint List
The Power Apps Patch function is used to modify single or multiple records of a data source. In other words, it updates the records in a data source without affecting other properties.
For example, I have a SharePoint Online list named “Issue Tracker” with the fields below.
Column Name | Data Type |
IssueTitle | It is a default single line of text |
ProductName | A single line of text |
In Power Apps, I have created a collection named “colIssues” having some records. Follow the code below.
OnStart = ClearCollect(
colIssues,
{
IssueTitle: "Laptop is not working",
ProductName: "Laptop"
},
{
IssueTitle: "Mobile is not working",
ProductName: "Mobile"
},
{
IssueTitle: "Outlook data fail",
ProductName: "Outlook"
}
);
Where,
- colIssues = Power Apps collection name
- IssueTitle, ProductName = Collection headers or columns
I want to patch these collection records to the SharePoint Online list using the ForAll function. To do so, insert a Button control on the Power Apps screen and set its OnSelect property to the code below.
OnSelect = ForAll(
colIssues,
Patch(
'Issue Tracker',
Defaults('Issue Tracker'),
{
Title: IssueTitle,
ProductName: ProductName
}
)
)
Where,
- Issue Tracker = SharePoint Online list
Once we click on the button control, the collection records will be saved in the SharePoint list, as shown below.
Output:
This is how we can work with the Power Apps ForAll Patch SharePoint Online List.
Power Apps ForAll Current Item
I have a Power Apps collection named “colProducts” with the fields ProductID, ProductName, and Price. Follow the code below.
OnStart = ClearCollect(
colProducts,
{
ProductID: "SP001",
ProductName: "Laptop",
Price: 100
},
{
ProductID: "SP002",
ProductName: "Mobile",
Price: 200
},
{
ProductID: "SP003",
ProductName: "Watch",
Price: 300
}
)
Now, insert a button control and set its Item property in the code to update the price by 0.9 (which applies a 10% discount).
OnSelect = ForAll(colProducts,
Patch(colProducts, ThisRecord, {Price: Price * 0.9})
)
Once you click on the button control, the ThisRecord property refers to the current item in the ForAll loop, and the price is updated by multiplying the original price by a 10% discount.
This is how we can use the Power Apps ForAll Current Item.
Some more Power Apps articles you may also like:
- Send Email From Power Apps
- 4 Various Power Apps Mod Function Examples
- Power Apps Naming Conventions
- Download File From SharePoint Document Library in Power Apps
- Power Apps Get Office 365 Group Members
I trust this Power Apps tutorial is useful. If you have any requirements related to the Power Apps ForAll function, you can follow this post until the end to get all the information with real-time scenarios.
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com