cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Paul112
New Member

How to use ForAll() inside ForAll()

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

1 ACCEPTED SOLUTION

Accepted Solutions

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 !

View solution in original post

7 REPLIES 7
CNT
Super User
Super User

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

RandyHayes
Super User
Super User

@Paul112 

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.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

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 !

@Paul112 

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

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

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
New Member

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

Paul112
New Member

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

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

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Carousel_PP_768x460_Wave2 (1).png

2022 Release Wave 2 Plan

Power Platform release plan for the 2022 release wave 2 describes all new features releasing from October 2022 through March 2023.

365 EduCon 768x460.png

Microsoft 365 EduCon

Join us for two optional days of workshops and a 3-day conference, you can choose from over 130 sessions in multiple tracks and 25 workshops.

Top Solution Authors
Users online (5,790)