web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Issues with ForAll fun...
Power Apps
Unanswered

Issues with ForAll function

(0) ShareShare
ReportReport
Posted on by 2

Hello,

 

I have a formula which worked great but started having issues when the datatable started growing. Data is stored in Dataverse.

 

So what the formula does is it checks if it has patched something already and ignores the patch if it has. It patches a certain number of new items to dataverse. This number is defined in a gallery row. ForAll then completes this formula for the specified amount of times.

 

Before issues I had Last(RIVITIEDOT).ID instead of First(Sort(RIVITIEDOT; Luotu; SortOrder.Descending)).ID in my formula. But the Last function stopped showing the true last record of the table.

 

The issue Im having now is in ForAll function where First(Sort(RIVITIEDOT; Luotu; SortOrder.Descending)).ID doesn't fetch the last record in time and it results in patching same ID multiple times. Is there a way to make ForAll function wait before it continues the sequence? Or is there a better way to do this?

 

Formula below:

 

Select(Parent);;
If(LookUp(RIVITIEDOT; Työnumero = ThisItem.ID; Työnumero) <> ThisItem.ID;
Patch(RIVITIEDOT; {ID: First(Sort(RIVITIEDOT; Luotu; SortOrder.Descending)).ID + 1; Työnumero: ThisItem.ID; Rivinumero: First(Sort(RIVITIEDOT; Luotu; SortOrder.Descending)).ID + 1});;
ForAll(Sequence(ThisItem.Määrä - 1); (If(Value(ThisItem.Määrä) > 1; Patch(RIVITIEDOT; {ID: First(Sort(RIVITIEDOT; Luotu; SortOrder.Descending)).ID + 1; Työnumero: ThisItem.ID; Rivinumero: First(Sort(RIVITIEDOT; Luotu; SortOrder.Descending)).ID + 1})))));;
Navigate(Screen_Rivitiedot; ScreenTransition.Cover)

 

 

BR,

Tomi

Categories:
I have the same question (0)
  • BCLS776 Profile Picture
    8,994 Moderator on at

    I think you're running into delegation issues preventing you from getting the correct ID. ForAll() is not delegable, so the app is trying to load all the records and stops at the delegation limit for the app. Try this to help:

     

    Select(Parent);;
    With({ nextID: First(Sort(RIVITIEDOT; Luotu; SortOrder.Descending)).ID + 1},
    If(
     LookUp(RIVITIEDOT; Työnumero = ThisItem.ID; Työnumero) <> ThisItem.ID;
     Patch(RIVITIEDOT; // you appear to be missing a second argument, perhaps Defaults(RIVITIEDOT) or a LookUp()
     {
     ID: nextID; 
     Työnumero: ThisItem.ID; 
     Rivinumero: nextID
     });;
     ForAll(Sequence(ThisItem.Määrä - 1); 
     (
     If(Value(ThisItem.Määrä) > 1; 
     Patch(RIVITIEDOT; // you appear to be missing a second argument, perhaps Defaults(RIVITIEDOT) or a LookUp()
     {
     ID: nextID; 
     Työnumero: ThisItem.ID; 
     Rivinumero: nextID
     }
     )
     )
     )
    ));;
    Navigate(Screen_Rivitiedot; ScreenTransition.Cover)

    That's free-typed, but it should get you closer. Make sure to include a second argument in your Patch() functions.

     

    Hope that helps,

    Bryan

  • TomiKasurinen Profile Picture
    2 on at

    This resulted in patching the same ID for all records.

     

    I thought Patch() didn't need a second argument when making new items.

  • BCLS776 Profile Picture
    8,994 Moderator on at

    @TomiKasurinen wrote:

    This resulted in patching the same ID for all records.

     

    I thought Patch() didn't need a second argument when making new items.


    Patch() uses two arguments when merging records; however, since you are creating items you need three arguments. The Defaults() function is what tells Patch() you are making a new record rather than updating an existing one.

     

    To increment the ID as the ForAll() moves through the records, try this:

     

    Select(Parent);;
    With({ nextID: First(Sort(RIVITIEDOT; Luotu; SortOrder.Descending)).ID + 1},
    If(
     LookUp(RIVITIEDOT; Työnumero = ThisItem.ID; Työnumero) <> ThisItem.ID;
     Patch(RIVITIEDOT; // you appear to be missing a second argument, perhaps Defaults(RIVITIEDOT) or a LookUp()
     {
     ID: nextID; 
     Työnumero: ThisItem.ID; 
     Rivinumero: nextID
     });;
     ForAll(Sequence(ThisItem.Määrä - 1) As aIndex; 
     (
     If(Value(ThisItem.Määrä) > 1; 
     Patch(RIVITIEDOT; // you appear to be missing a second argument, perhaps Defaults(RIVITIEDOT) or a LookUp()
     {
     ID: nextID; 
     Työnumero: ThisItem.ID; 
     Rivinumero: nextID + aIndex
     }
     )
     )
     )
    ));;
    Navigate(Screen_Rivitiedot; ScreenTransition.Cover)

     

    Bryan

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 325 Most Valuable Professional

#2
11manish Profile Picture

11manish 165

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 88 Super User 2026 Season 1

Last 30 days Overall leaderboard