ForAll Workarounds in Canvas App (2025)

ForAll Workarounds in Canvas App (1)

  • Report this article

Maham Khan ForAll Workarounds in Canvas App (2)

Maham Khan

Senior Power Platform Consultant at Systems Limited | MCP

Published Sep 2, 2022

+ Follow

Background:

As you know we cannot clear a collection and use Set to set a variable within ForAll function in canvas app. So, We've found workarounds to fix these two issues. We'll be taking separate examples to explain the workaround for each of the issues. The key is to use collect for both but with different approaches.

Let's start

1. Set a variable and use within ForAll example:

So for this, we will not be using the Set function we will use collect as we know collect works within ForAll. In this example, I'll be auto-incrementing numbers to insert in my SharePoint list which goes like 1000,2000,3000, and so on , having a difference of 1000. Following are the steps you need to take to achieve the desired result:

  1. Before the ForAll we're going to add a ClearCollect to define the starting number and name it as lineno having one field itemno

ClearCollect(lineno,{itemno:1000}); 

2. In ForAll, we are trying to patch records in a SharePoint list that contains a field lineno for auto-incremented number. So, we are going to use Last function to patch the lastly inserted row in lineno Collection to that field.

ForAll(ItemGallery_4.AllItems,Patch(SuppliersSubmission,Defaults(SuppliersSubmission),{ Lineno:Last(lineno).itemno});) 

3. After this we are going to add a collect to update our lineno collection so that it contains the next number in the sequence. We will add the code within ForAll after the patch to update it after each patch and generate the number for next row to be inserted. We will add a new row in lineno collection by adding the lastly inserted row with the incremented value of 1000. The code should look like this:

ForAll(ItemGallery_4.AllItem,Patch(SuppliersSubmission,Defaults(SuppliersSubmission),{ Lineno:Last(lineno).itemno});Collect(lineno,{itemno:Last(lineno).itemno+1000});) 

And We're done. Wasn't that easy :)

Let's move to the next example It's going to be a little more complicated than the first one but very interesting.

1. Clear Collection within ForAll example:

So the trick for this one is to use the RemoveIf function. We usually use this when we're trying to insert multiple records in one list (Parent table) and then insert multiple records (child table) against those inserted records. In simpler words inserting records in lists having one-to-many relationship but inserting it multiple times within one go. Please note that we already have a collection containing nested records (multiple records against one or more records).

Recommended by LinkedIn

PowerApps Express Designer: Turn a wireframe into an… Anish Arora 2 years ago
InfoPath like Forms in PowerApps Sanjay Kumar Pathak 5 years ago
Adobe Enters the AI Ring: Acrobat's New Assistant… Crosslin Technologies 7 months ago
Tip no 1: clear all the collection you're using to collect data within ForAll.

  1. For this what we will do is we will collect the header table while patching it to link it with the child list records while inserting.

ForAll(insertPRcol As refrecord,Collect(temprequestheadercol,Patch(PurchaseRequisitionRequest,Defaults(PurchaseRequisitionRequest),{Title:Text(Now(),"yyyy-mm-ddThhmmssfff"),DepartmentName:"IT",RequestType:"PR",PurchaseRequestType:"Competitive"});//Request Header Patch ends here)); //ForAll ends here 

2. Now within the for all for child list we could do the last thing we did in first example to set the lookup column in child list.

//Patch to insert in child listForAll(refrecord.BPOItemlist As temprecord,Collect(tempitemscol,Patch(PurchaseRequisitionItem,Defaults(PurchaseRequisitionItem),{ ItemCode:Text(temprecord.ItemVItemcode.Text), ItemDescription:Text(temprecord.ItemVItemDescription.Text), UOM:Text(temprecord.ItemVUOM.Text), Quantity:Value(temprecord.ItemVQTY.Text), EstimatedPrice:Value(temprecord.ItemVPrice.Text), Currency:Text(temprecord.Currencylabel.Text), Amount:Value(temprecord.ItemVVistaPrice.Text), PRForm:{ Id: Last(temprequestheadercol).ID, Value: Last(temprequestheadercol).Title }, BudgetItemCode:"BUD0013"})));//Patch to insert in child list ends here 

The combined code should look like this

ForAll(insertPRcol As refrecord,Collect(temprequestheadercol,Patch(PurchaseRequisitionRequest,Defaults(PurchaseRequisitionRequest),{Title:Text(Now(),"yyyy-mm-ddThhmmssfff"),DepartmentName:"IT",RequestType:"PR",PurchaseRequestType:"Competitive"});//Request Header Patch ends here)//Patch to insert in child listForAll(refrecord.BPOItemlist As temprecord,Collect(tempitemscol,Patch(PurchaseRequisitionItem,Defaults(PurchaseRequisitionItem),{ ItemCode:Text(temprecord.ItemVItemcode.Text), ItemDescription:Text(temprecord.ItemVItemDescription.Text), UOM:Text(temprecord.ItemVUOM.Text), Quantity:Value(temprecord.ItemVQTY.Text), EstimatedPrice:Value(temprecord.ItemVPrice.Text), Currency:Text(temprecord.Currencylabel.Text), Amount:Value(temprecord.ItemVVistaPrice.Text), PRForm:{ Id: Last(temprequestheadercol).ID, Value: Last(temprequestheadercol).Title }, BudgetItemCode:"BUD0013"})));//Patch to insert in child list ends here); //ForAll ends here 

3. After this we will remove the record from the temporay collection using RemoveIf

RemoveIf(temprequestheadercol,ID=Last(temprequestheadercol).ID); 

the combined code should look like this

ForAll(insertPRcol As refrecord,Collect(temprequestheadercol,Patch(PurchaseRequisitionRequest,Defaults(PurchaseRequisitionRequest),{Title:Text(Now(),"yyyy-mm-ddThhmmssfff"),DepartmentName:"IT",RequestType:"PR",PurchaseRequestType:"Competitive"});//Request Header Patch ends here)//Patch to insert in child listForAll(refrecord.BPOItemlist As temprecord,Collect(tempitemscol,Patch(PurchaseRequisitionItem,Defaults(PurchaseRequisitionItem),{ ItemCode:Text(temprecord.ItemVItemcode.Text), ItemDescription:Text(temprecord.ItemVItemDescription.Text), UOM:Text(temprecord.ItemVUOM.Text), Quantity:Value(temprecord.ItemVQTY.Text), EstimatedPrice:Value(temprecord.ItemVPrice.Text), Currency:Text(temprecord.Currencylabel.Text), Amount:Value(temprecord.ItemVVistaPrice.Text), PRForm:{ Id: Last(temprequestheadercol).ID, Value: Last(temprequestheadercol).Title }, BudgetItemCode:"BUD0013"})));//Patch to insert in child list ends hereRemoveIf(temprequestheadercol,ID=Last(temprequestheadercol).ID);); //ForAll ends here 

And that's it. I'm going to end this belong with one more tip which is

Tip no 2 : Always use As in ForAll especially when you're removing records in that ForAll.

As for those who don't know what As in ForAll is, it's basically creating an Alias to access records in replacement of ThisRecord. Whatever you set as an Alias will be used in replacement of ThisRecord. For example, in the main ForAll, we have an Alias refrecord and in the child ForAll we have temprecord as an Alias.

I hope this helps. Thank you for reading my article :)

Like
Comment

55

15 Comments

Art Karp

Solution Architect - Eventix Apps for Power Platform - Dynamics 365 (CE/CRM)

1y

  • Report this comment

A blog post I came across also describes how ForAll works..."You may ask why we named our new function ForAll instead of For or ForEach as many other languages do.That’s because our system is designed for asynchronous and parallel operations.The formula for one record may be paused waiting on a data operation, and while we wait, we may start evaluating the formula for another record.The best way to think about it is that we could be evaluating the formula for all the records at the same time, with the implication that we can’t guarantee the order in which the formula for each record will be evaluated.For most functions and services that are stateless and do not have side effects, including those used above, this is perfectly fine and allows us to optimize performance.We are hoping that the All in the function name will differentiate it from other For constructs that imply an order."You can see the full post here...https://powerapps.microsoft.com/en-us/blog/performance-refresh-forall-multiple-field-lookups-531/

Like Reply

1Reaction

Art Karp

Solution Architect - Eventix Apps for Power Platform - Dynamics 365 (CE/CRM)

1y

  • Report this comment

Per the MS docs on ForAll..."When writing your formula, keep in mind that records can be processed in any order and, when possible, in parallel. The first record of the table may be processed after the last record."If two records are processed in parallel, Last(temprequestheadercol).ID might not be the record you are expecting. I believe that if you process and create all the parent records first and after that completes, you use ForAll on the parent records to create the child records. One issue I have found with Dataverse is that you need a unique ID (GUID) that is created for each record in your collection. Then that ID is included when Patching the header records to Dataverse. This way your ID will be passed back from Dataverse in the Patch return collection. This would also be the parent ID of items in the child collection. Since your parent ID is also in the child records, you can cross reference them to the entiy record IDs that were created in Dataverse. In your code you used "Last". I am wondering if the ForAll processed multiple records in parallel if the outcome would be different than using "First". The problem is there is no way to control a test for this. You can't force the process in parallel

Syed Adil Hassan Naqvi

Power Platform Consultant at Systems Limited(Techvista) @ENBD | PowerApps | SharePoint | Azure | CRM | Power Pages | Power Platform | PowerBI | JavaScript | NodeJS | React JS | PCF | Web Services Creation and Maintenance

2y

  • Report this comment

Very Informative MA

Like Reply

1Reaction 2Reactions

Abrar Ahmad

Microsoft certified D365 Dev | Shopper value| Power Bi - Data Science learner

2y

  • Report this comment

very informative

Like Reply

1Reaction 2Reactions

See more comments

To view or add a comment, sign in

More articles by Maham Khan

  • Converting Navigation Menu Bar Using SVG via Figma into Component Part 2

    ForAll Workarounds in Canvas App (19)

    Jul 9, 2022

    Converting Navigation Menu Bar Using SVG via Figma into Component Part 2

    Background: We have already designed a navigation menu bar using a gallery. You can view it using this link to Part 1…

    ForAll Workarounds in Canvas App (20) ForAll Workarounds in Canvas App (21) ForAll Workarounds in Canvas App (22) 47

    2 Comments

  • Implementing Navigation Menu Bar Using SVG via Figma Part 1

    ForAll Workarounds in Canvas App (23)

    Jul 3, 2022

    Implementing Navigation Menu Bar Using SVG via Figma Part 1

    Background: We had a requirement in our ongoing project in which we have to create a navigation menu bar and our client…

    ForAll Workarounds in Canvas App (24) ForAll Workarounds in Canvas App (25) ForAll Workarounds in Canvas App (26) 83

    11 Comments

  • Get SharePoint Group ID within Power Automate

    ForAll Workarounds in Canvas App (27)

    Oct 11, 2021

    Get SharePoint Group ID within Power Automate

    Background: I have a list in which there are fields having data types “Person or Group”, mostly I’m storing groups in…

    ForAll Workarounds in Canvas App (28) ForAll Workarounds in Canvas App (29) ForAll Workarounds in Canvas App (30) 24

    6 Comments

  • Attachment Limitations and Workarounds in Canvas Apps

    ForAll Workarounds in Canvas App (31)

    Oct 5, 2021

    Attachment Limitations and Workarounds in Canvas Apps

    Background: We catered these limitations while we need to create an editable gallery to store/edit a record which also…

    ForAll Workarounds in Canvas App (32) ForAll Workarounds in Canvas App (33) ForAll Workarounds in Canvas App (34) 48

    21 Comments

Sign in

Stay updated on your professional world

Sign in

By clicking Continue to join or sign in, you agree to LinkedIn’s User Agreement, Privacy Policy, and Cookie Policy.

New to LinkedIn? Join now

Insights from the community

  • Presentations You need to create charts and graphs for your presentation. What tools should you use?
  • Data Visualization How can you customize ggplot2 templates to match your branding?
  • Critical Thinking What are the best tools or software for creating and sharing argument maps?
  • Critical Thinking You want to create visualizations for your website. What are the best software options?
  • Application Development How can you design accessible dashboards in web applications?
  • Data Visualization How do you avoid cluttering your dashboard with too many charts and widgets?
  • Graphic Design Software How do you create dynamic and interactive charts and diagrams in Excel?
  • Critical Thinking You want to create interactive visualizations for your business. How can you do it with ease?
  • Decision-Making You want to create interactive dashboards for your business. What’s the best way to get started?

Others also viewed

  • Tag-It! - Implementing custom XMP metadata Dan Hudson 7y
  • Microsoft's Power Pages: The Ultimate Business Tool Alice Wickham 1y
  • 🎨 𝗛𝗼𝘄 𝘁𝗼 𝘂𝘀𝗲 𝗴𝗿𝗮𝗱𝗶𝗲𝗻𝘁𝘀 𝗶𝗻 𝗠𝗶𝗰𝗿𝗼𝘀𝗼𝗳𝘁 𝗟𝗶𝘀𝘁𝘀 Federico Sapia 1y
  • Analytics basics Ritu Yadav 5y
  • Adobe Experience Manager - using OSGI configurations in the client Nicolas Peltier 6y
  • Dynamics 365 Business Edition inspires UI re-think - Microsoft nails it? Anton W. 7y
  • Multiple trended fallouts on a single graph in Adobe Workspace Keith Yap 5y
  • 15 plugins for Figma to visualize your data Tetiana Sydorenko 1y
  • Embedded Canvas Apps within Model-Driven Forms Robert Bailey (Msc) 5y

Explore topics

  • Sales
  • Marketing
  • IT Services
  • Business Administration
  • HR Management
  • Engineering
  • Soft Skills
  • See All
ForAll Workarounds in Canvas App (2025)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Terrell Hackett

Last Updated:

Views: 6326

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.