cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
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
Highlighted
Super User
Super User

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

@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. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.

View solution in original post

11 REPLIES 11
Highlighted
Super User
Super User

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

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.

Highlighted
Frequent Visitor

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

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 😉

Highlighted
Super User
Super User

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

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.

Highlighted
Super User
Super User

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

@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. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Highlighted
Frequent Visitor

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

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 ? 😉 

 

Highlighted
Frequent Visitor

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

 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.

 

Highlighted
Super User
Super User

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

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.

Highlighted
Super User
Super User

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

@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. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Highlighted
Frequent Visitor

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

@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
secondImage

New Return to Workplace

Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.

August 2020 CYST Challenge

Check out the winners of the recent 'Can You Solve These?' community challenge!

Experience what’s next for Power Apps

Join us for an in-depth look at the new Power Apps features and capabilities at the free Microsoft Business Applications Launch Event.

Check this Out

Helpful information

Featuring samples like Return to the Workplace and Emergency Response Applications

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Users online (4,975)