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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / How to use ForAll() in...
Power Apps
Unanswered

How to use ForAll() inside ForAll()

(1) ShareShare
ReportReport
Posted on by

Hello all,
I would like to know how to use ForAll() function inside another ForAll().
Inside ForAll() function, we can use ThisRecord property to retrieve selected record values.
It seems to be allowed to nest second level ForAll() function, but when we do it, ThisRecord property retrieve only last ForAll() level. So, my questions are
- Is it really allowed to nest ForAll() function?
- how to retrieve first level ForAll() values inside second ForAll() level?
If it is not allowed to nest ForAll() function, which formula (or tips) should be used to deliver same result? (meaning Loop insinde another loop)

Thank's

Categories:
I have the same question (0)
  • CNT Profile Picture
    10,921 Super User 2024 Season 1 on at

    @Anonymous  Use As for the Outer lever and refer the outer level using that. ForAll(firstLoop As varFirstLoop ......

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @Anonymous 

    Keep in mind that a ForAll is not a For/Loop.  A ForAll is a function that returns a table based on iterations over another table.  So, if you are looking to do what you are doing to try and make a ForAll into a ForLoop, your challenges are ahead with the concept of a for/loop inside of a for/loop because again...ForAll is a table function.  Can they be nested...sure, if you need a table within a table, it is perfectly fine.  

    As for referring to tables - PowerApps as has As statement that can be used to provide a named reference to your table (i.e. the ForAll).  

    As for ThisRecord - ThisRecord refers to the closes table operation function.  So, ThisRecord inside of a ForAll refers to the current iteration record of the ForAll.  If you have, let's say, a Filter inside of the ForAll to Filter another table to include in your table record of the ForAll, then ThisRecord inside of the Filter would refer to the Filter record.  Best to use the As statement to be clear on what you are referring to.

     

    I hope this is helpful for you.

     

  • Verified answer
    Community Power Platform Member Profile Picture
    on at

    Thank you @CNT and @RandyHayes to your reactivity.
    It is really appreciated !


    I'm using tables on each ForAll() level :  first ForAll() will give parameters to the filter used to the second ForAll().

    Thank you !

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @Anonymous 

    Sounds good.  If you get stuck, feel free to post back your formula and I'd be happy to troubleshoot and correct it.

  • tssykmr Profile Picture
    2 on at

    I am trying to do something similar.. I have 2 items in gallery3 and a few modifiers that are different for each gallery item.  The patch is overwriting both item 1 and 2 from the gallery with the modifiers only for item 2 instead of the correct respective modifiers (modifiers for item 1 to item 1). This is my formula: 

     

    ForAll(Gallery3.AllItems As yomom,ForAll(LookUp(fdcs559,fdcId=LookUp('Food Nicknames',fghfh=yomom[@Ingredient],fdcId),foodPortions[@modifier]) As hmm,Patch(units7,LookUp(units7,Value=yomom.Ingredient),{Unit2: RenameColumns(Split(Concatenate(Concat(LookUp(units7,Value = yomom[@Ingredient]).Unit2,Unit,"#"),"#",hmm.modifier),"#"),"Result","Unit")})));

     

    Please help, thanks!

  • dcreddycrm Profile Picture
    2 on at

    Hi,
    I am trying to capture the current record value of the Outer ForAll in the Inner ForAll. I have tried numerous ways but no luck. 

    // for single user it's perfectly working
    Set(NewUserRecord,LookUp(Users, 'User Name' = cmbCopyTo.Selected.'User Name'));

    Clear(copyFromUserRoles);
    ForAll(LookUp(Users, 'User Name' = cmbCopyFrom.Selected.'User Name').'Security Roles (systemuserroles_association)',
    Collect(copyFromUserRoles,
    {
    CloneRoleId: Role,
    CloneRoleName: Name
    })
    );

    ForAll(copyFromUserRoles,Relate(NewUserRecord.'Security Roles (systemuserroles_association)',
    LookUp('Security Roles',
    Name = CloneRoleName && businessunitid.name = NewUserRecord.'OOB Business Unit'.Name
    ))
    );

    // I need to do this for multi users

    Clear(newUsers);
    ForAll(cmbCopyTo.SelectedItems, Collect(newUsers,LookUp(Users, 'User Name' = cmbCopyTo.Selected.'User Name')));

    Clear(copyFromUserRoles);
    ForAll(LookUp(Users, 'User Name' = cmbCopyFrom.Selected.'User Name').'Security Roles (systemuserroles_association)',
    Collect(copyFromUserRoles,
    {
    CloneRoleId: Role,
    CloneRoleName: Name
    })
    );

    // I need something like this. I want to loop through all the newUsers collection items and assign the same security roles to all those users.
    ForAll(newUsers,
    ForAll(Assign Security Roles to each user from the outer forall)
    );

    ForAll(copyFromUserRoles,Relate(NewUserRecord.'Security Roles (systemuserroles_association)',
    LookUp('Security Roles',
    Name = CloneRoleName && businessunitid.name = NewUserRecord.'OOB Business Unit'.Name
    ))
    );

    Could someone please help me?

    Thanks,

    dcr

  • Community Power Platform Member Profile Picture
    on at

    Hi,
    Maybe this will help you to find your answer :
    https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/operators#thisitem-thisrecord-and-as-operators

    ForAll() function returns each row with ThisRecord that can be used like this :
    ForAll(MyCollection, Collect(ColResult:{value:ThisRecord.value}))

    Nested ForAll() must use As to distinguish records with a personal name. It can be used like this :

    ClearCollect(colNumbers,{value:67},{value:66},{value:65});
    ClearCollect(colASCII,{letter:"A",value:65},{letter:"B",value:66},{letter:"C",value:67});
    Clear(colResult);

    ForAll(colNumbers As theNum,
         ForAll(Filter(colASCII,value=theNum.value) As theText,
              Collect(colResult,{result:theText.letter})
         )
    )

    ColResult will contains respectively : C, B, A

     

    I hope this will help you

  • Community Power Platform Member Profile Picture
    on at

    i m trying to loop through all the day of the week and also loop through all project and trying to insert one row for each. So for 7 days of the week and 5 project. I would need to insert 35 records , user name , date , hours and project name 

    ForAll(DaysOfWeek,
    ForAll(Projects,
    {
    // Calculate the current date and hours based on the day of the week
    Set(CurrentDate, DateAdd(SelectedDate, Switch(
    DaysOfWeek[Result],
    "Sunday", -1,
    "Monday", 0,
    "Tuesday", 1,
    "Wednesday", 2,
    "Thursday", 3,
    "Friday", 4,
    "Saturday", 5
    ), Days)),

    Set(CurrentHours, Switch(
    DaysOfWeek[Result],
    "Sunday", SundayHours.Text,
    "Monday", MondayHours.Text,
    "Tuesday", TuesdayHours.Text,
    "Wednesday", WednesdayHours.Text,
    "Thursday", ThursdayHours.Text,
    "Friday", FridayHours.Text,
    "Saturday", SaturdayHours.Text
    ));

    // Check if a record already exists in the 'TimeTracker Report'
    Set(ExistingRecord, LookUp(
    'TimeTracker Report',
    UserName = UserFullName && Date = CurrentDate && ProjectName = Project.Name
    ));

    // If no record found, insert a new record; otherwise, update the existing record
    If(
    IsBlank(ExistingRecord),
    Patch(
    'TimeTracker Report',
    Defaults('TimeTracker Report'),
    {
    Title: "Test",
    UserName: UserFullName,
    ProjectName: Project.Name,
    Date: CurrentDate,
    Hours: Value(CurrentHours)
    }
    ),
    Patch(
    'TimeTracker Report',
    ExistingRecord,
    {
    Title: "Test",
    Hours: Value(CurrentHours)
    }
    )
    )
    }
    )
    )

     

    Cna you please correct this query? I m gettting Forall function is having invalid argument 

     

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 711 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 319 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard