cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Alaa-Alarori
Level: Powered On

Update a column value in a collection using ForAll

Hi,

I am using ForAll to loop through a collection, I would like to change a column value for the referenced record based on a condition

I read about Record Scope and it seems the following code should work

ForAll(CommunityProfile, If(Condition, {Community :"AA"}))

I also tried the following:

ForAll(CommunityProfile, If(Condition, CommunityProfile[@Community] = "AA"))

and they both didn't work 

 

any help!

 

Thanks

2 ACCEPTED SOLUTIONS

Accepted Solutions
Super User
Super User

Re: Update a column value in a collection using ForAll

@Alaa-Alarori 

One thing to always keep in mind about ForAll, is that it is really not entirely like a For Loop that you might be used to in a programing environment. Primarily - ForAll *returns* a table.  The action you take within the ForAll determines the "shape" of the table returned.  ForAll's can be used to do actions within them that impact data, but they require more steps to do so.

 

So, in your first formula:

ForAll(CommunityProfile, If(Condition, {Community :"AA"}))

Would return a Record with a Community column with the value "AA" for every record in CommunityProfile where the Condition was true.  If there were 5 records that met the condition in CommunityProfile, you would have a table with 5 rows, with one column called Community and all with the value of "AA".  HOWEVER, you didn't actually do anything with the results of the ForAll...so, nothing happened (not that it was going to be what you wanted anyway).

 

Your second formula just wouldn't work anyway!  You can't assign values quite like that. So we won't go much more on that one.

 

NOW...what you want to do.  You have a few options.  One option is to simply use an UpdateIf function on your collection.  So, something like this:

UpdateIf(CommunityProfile, Condition, {Community:"AA"})

This would actually update the Community column with "AA" in any record in CommunityProfile that meets the condition you have.  (Also note: the UpdateIf will return a table of modified records, should you need to capture the results for some reason.)

 

The other option is to use the ForAll, but it is much more challenging.  The reason is, you would either Patch/Update the record currently being referenced in the ForAll, or re-shape a resulting table to return from your ForAll.

If you use the ForAll to Patch/Update, then you need to have a reference to the current record.  Within the ForAll, you can make use of any of the fields/columns of the current record in the ForAll, but you cannot reference the entire record.  So, if your data has some identifying component to it (like an ID), then you can reference that to lookup and Patch/Update the actual value in the datasource.  Something like:

ForAll(CommunityProfile, 
         If(Condition, 
              Patch(CommunityProfile, 
              Lookup(CommunityProfile, ID=CommunityProfile[@ID]), 
              {Community:"AA"}
              )
            )
        )

You can see that gets more complex.

 

That second option in the ForAll is to re-shape the data and return it in your ForAll.  This would involve returning either the same record schema (which you'd have to type out for each column) or a different altered schema.

 

But...I don't believe that (based on your original question and perceived intention) this is what you want to do.

 

I hope this is clear and helpful for you.

 

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Community Support Team
Community Support Team

Re: Update a column value in a collection using ForAll

Hi @Alaa-Alarori ,

Do you want to changed a column value in for the referenced record within the ForAll loop based on a Condition?

 

Based on the formula that you mentioned, I think there is something wrong with it. In addition, I think the Disambiguation operator (@) could not achieve your needs within your ForAll formula.

I have made a test on my side, I think the RenameColumns function could achieve your needs, please modify your formula as below:

 

ForAll(
RenameColumns(CommunityProfile, "PrimaryColumn", "PrimaryColumn1"),
If(
Condition,
Patch( /* <-- Use Patch function to update value */
CommunityProfile,
LookUp(CommunityProfile, PrimaryColumn = PrimaryColumn1),
{
Community: "AA"
}
)
)
)

Note: The PrimaryColumn represents the column in your CommunityProfile collection, which used to identify a record uniquely.

 

More details about the RenameColumns function in PowerApps, please check the following article:

RenameColumns function

 

Best regards,

 

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
3 REPLIES 3
Super User
Super User

Re: Update a column value in a collection using ForAll

@Alaa-Alarori 

One thing to always keep in mind about ForAll, is that it is really not entirely like a For Loop that you might be used to in a programing environment. Primarily - ForAll *returns* a table.  The action you take within the ForAll determines the "shape" of the table returned.  ForAll's can be used to do actions within them that impact data, but they require more steps to do so.

 

So, in your first formula:

ForAll(CommunityProfile, If(Condition, {Community :"AA"}))

Would return a Record with a Community column with the value "AA" for every record in CommunityProfile where the Condition was true.  If there were 5 records that met the condition in CommunityProfile, you would have a table with 5 rows, with one column called Community and all with the value of "AA".  HOWEVER, you didn't actually do anything with the results of the ForAll...so, nothing happened (not that it was going to be what you wanted anyway).

 

Your second formula just wouldn't work anyway!  You can't assign values quite like that. So we won't go much more on that one.

 

NOW...what you want to do.  You have a few options.  One option is to simply use an UpdateIf function on your collection.  So, something like this:

UpdateIf(CommunityProfile, Condition, {Community:"AA"})

This would actually update the Community column with "AA" in any record in CommunityProfile that meets the condition you have.  (Also note: the UpdateIf will return a table of modified records, should you need to capture the results for some reason.)

 

The other option is to use the ForAll, but it is much more challenging.  The reason is, you would either Patch/Update the record currently being referenced in the ForAll, or re-shape a resulting table to return from your ForAll.

If you use the ForAll to Patch/Update, then you need to have a reference to the current record.  Within the ForAll, you can make use of any of the fields/columns of the current record in the ForAll, but you cannot reference the entire record.  So, if your data has some identifying component to it (like an ID), then you can reference that to lookup and Patch/Update the actual value in the datasource.  Something like:

ForAll(CommunityProfile, 
         If(Condition, 
              Patch(CommunityProfile, 
              Lookup(CommunityProfile, ID=CommunityProfile[@ID]), 
              {Community:"AA"}
              )
            )
        )

You can see that gets more complex.

 

That second option in the ForAll is to re-shape the data and return it in your ForAll.  This would involve returning either the same record schema (which you'd have to type out for each column) or a different altered schema.

 

But...I don't believe that (based on your original question and perceived intention) this is what you want to do.

 

I hope this is clear and helpful for you.

 

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Community Support Team
Community Support Team

Re: Update a column value in a collection using ForAll

Hi @Alaa-Alarori ,

Do you want to changed a column value in for the referenced record within the ForAll loop based on a Condition?

 

Based on the formula that you mentioned, I think there is something wrong with it. In addition, I think the Disambiguation operator (@) could not achieve your needs within your ForAll formula.

I have made a test on my side, I think the RenameColumns function could achieve your needs, please modify your formula as below:

 

ForAll(
RenameColumns(CommunityProfile, "PrimaryColumn", "PrimaryColumn1"),
If(
Condition,
Patch( /* <-- Use Patch function to update value */
CommunityProfile,
LookUp(CommunityProfile, PrimaryColumn = PrimaryColumn1),
{
Community: "AA"
}
)
)
)

Note: The PrimaryColumn represents the column in your CommunityProfile collection, which used to identify a record uniquely.

 

More details about the RenameColumns function in PowerApps, please check the following article:

RenameColumns function

 

Best regards,

 

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Alaa-Alarori
Level: Powered On

Re: Update a column value in a collection using ForAll

Hi @v-xida-msft 

this works like a charm!

thanks for the code example 

 

@RandyHayes 

Your explanation is aowsome, it's really beneficial to understand the result of each function, this will change the way I assemble formulas.

thanks for taking the time to explain in details

 

Best,

Alaa

Helpful resources

Announcements
firstImage

PowerApps Monthly Community Call!

Join us next Wednesday for our Demo Extravaganza, October 16, 2019 8am PDT.

firstImage

Microsoft Business Applications Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

FirstImage

Power Platform World Tour

Coming to a city near you

thirdimage

PowerApps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

FourthImage

Join PowerApps User Group!!

Connect, share, and learn with your peers year-round

SecondImage

Power Platform Summit North America

Register by September 5 to save $200

Top Kudoed Authors
Users Online
Currently online: 390 members 5,103 guests
Please welcome our newest community members: