cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
SimonEckler
Frequent Visitor

Patch the same data source as in Forall loop, does not work

Hi,

 

I search the forum and found an answer (https://powerusers.microsoft.com/t5/Building-Power-Apps/Update-a-column-value-in-a-collection-using-...) to my issue but it does not work. So here is my problem:


I have a collection with some records. I want to loop through it and Patch or UpdateIf a column within the same collection.

Or in other words. I want to loop through each record and want to change the status to "ok" 

 

ForAll( myCollection,
Patch( myCollection, LookUp(myCollection, ID= 10 ),
{ status : ok })
);

I always get the error:
the function cannot operate on the same datasource that is used in Forall

 

What I'm doing wrong ?

 

Thanks guys

1 ACCEPTED SOLUTION

Accepted Solutions

@SimonEckler 

So, the very easy solution is this...use a table-returning function!

 

It is little realized, but functions like Filter, AddColumns, RenameColumns, ShowColumns, etc. all return a NEW table of records.

So (and this is something you will run into anyway) the best choice is to rename the ID column (assuming you have one - if not, then some identifying unique column).

 

So, if you do:

ForAll(
    RenameColumns(myCollection, ID, "rowID"),

    <do your SQL patches here>

    UpdateIf(myCollection, ID=rowID, {status:"ok"})

)

Then the ForAll is working on a "copied" table of your original myCollection - and in this case the ID column is renamed to rowID (this is helpful in the UpdateIf function for disambiguation).

 

Or, to take it a step further, as you'll likely want to only "ForAll" on the records that need to be updated, then this:

ForAll(
    RenameColumns(Filter(myCollection, status<>"ok"), ID, "rowID"),

    <do your SQL patches here>

    UpdateIf(myCollection, ID=rowID, {status:"ok"})

)

 With a filter!

 

All the above will let you modify the myCollection collection because, in reality, the ForLoop is NOT based on it any longer.

 

I hope that is clear.

_____________________________________________________________________________________
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.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

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

View solution in original post

11 REPLIES 11
eka24
Super User III
Super User III

Change the  second collection myCollection to Datasource or Table name not a collection as:

 

ForAll( myCollection,
Patch( Datasource, LookUp(myCollection, ID= 10 ),
{ status : ok })
);

------------

If you like this post, give a Thumbs up. Where it solved your request, Mark it as a Solution to enable other users find it.

Hi,

 

thanks for your reply.

Sorry, but didn't get the point.

Do you mean I should change it to another datasource? My datasource is the collection 😉

What datasource are you using, Excel, sharepoint SQL etc and what is the name of the table you are sending the data to?

mycollection is a temporal table stored in powerapps. The formula you stated is Moving the Records from mycollection which is a temporal storage in Powerapps to a Permanent Datasource like Excel or Sharepoint.

 

The idea is to change the myCollection to the actual name of the table in your excel or sharepoint list

ForAll( myCollection,
Patch( Datasource, LookUp(myCollection, ID= 10 ),
{ status : ok }));

 

So what is the name of your Table you are sending the records to? Replace Datasource in the formula with that TableName

If you dont have any table like that, then Create a table with similar column names as in myCollection

------------

If you like this post, give a Thumbs up. Where it solved your request, Mark it as a Solution to enable other users find it.

@eka24 

I think the point being missed is that @SimonEckler stated:

I have a collection with some records. I want to loop through it and Patch or UpdateIf a column within the same collection.

If that is the case, then if you just want to change the status of all items in your collection to ok then you can do this:

UpdateIf(myCollection, true, {status:ok})

Although, I am not sure what "ok" is.  Is that a variable or do you mean to make that a string? If string then put quotes around it.  If it is intended to be bool, then use true or false.

 

You Lookup in the original formula didn't make much sense, so if there is something else you are trying to do with that...post back.

_____________________________________________________________________________________
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.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

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

Ok, understood.

So let me explain or ask it in another way.

I need a way to update my local collection to see which records in this collection have already been touched.

And you are right, my destination datasource which I have to patch is an Azure SQL.

BUT: I want to know which records from my local collection has already been patched to my Azure SQL.

So my idea is to update/patch each record in my local collection with the status: "ok" in a Forall loop after it has been patch to the Azure SQL.

 

Hope this is more clear now ? 😉 

 

 Hi Randy,

 

correct.

But with one little change:

 

If that is the case, then if you just want to change the status of the current item in your collection to ok then you can do this:

 

So after each record in the ForAll loop I want to change the status of the current item to "ok",  true or something else.

 

In that case follow the reply of @RandyHayes 

Thank @RandyHayes 

------------

If you like this post, give a Thumbs up. Where it solved your request, Mark it as a Solution to enable other users find it.

@SimonEckler 

What ForAll loop and current item are you referring to?  I believe we're missing some context here from you.

 

I am gathering by pure assumption that you have some ForAll loop that you are going through to patch to an AzureSQL datasource.  And, within that, you want to update your myCollection collection to indicate that it has been patched on the datasource.

 

If that assumption is correct, then just issues the UpdateIf like this:

UpdateIf(myCollection, ID=<whateverYouHavefromYourAzurePatch>, {status:"ok"})

 

BUT, my guess is that you are actually doing a ForAll loop on the myCollection to actually perform your Patches to Azure.  In which case...no, you cannot update the collection that the ForAll is based on.

 

Before I go too much further on something that might not apply...let me know if I am in the ballpark of what you are trying to do.

 

 

 

_____________________________________________________________________________________
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.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

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

@RandyHayes 

 

yes you got it 😀

BUT, my guess is that you are actually doing a ForAll loop on the myCollection to actually perform your Patches to Azure.  In which case...no, you cannot update the collection that the ForAll is based on.

Ok and thats why I get the error...

 

So I think there must be another way to manage this ?

If I go a little deeper in my explanation:

My App is also used offline. So I always save the data to a collection (and use SaveData) and at the end of the day I patch all the records to my Azure SQL with Forall.

If the user quits the App while the patch is running, I want to know which records have already been patched to the SQL and do the rest of the records later.

 

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

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

PowerPlatform 768x460.png

Microsoft Learn

Check out our new Discover Your Career Path blog post series and get all the details.

Users online (1,100)