cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Gelos
Post Partisan
Post Partisan

Patch update using collection

Hi guys I need your help.

 

I want to update cds table using collection. my collection will be

Clearcollect(collectionname,{id=1,name=geek,Age=20},{id=2,name=geek,Age=20},{id=3,name=geek,Age=20}))

I want to update that rows based on id I am using formula

ForAll(collectionname,

patch(tablename(First(

filter(tablename,Id in collectionname.id

)

),

{

Name=collectionname.name,

Age=collectionname.Age

}

)

)

But I will update only Id=1 row in table not Id=2 as wll as Id=3 

What is the correct formula please help me.

1 ACCEPTED SOLUTION

Accepted Solutions
WarrenBelz
Most Valuable Professional
Most Valuable Professional

Hi @Gelos ,

Assuming the Id fields line up

Clearcollect(
   collectionname,
   {
      id=1,
      name=geek,
      Age=20
      id=2,
      name=geek,
      Age=20
      id=3,
      name=geek,
      Age=20
   }
);
ForAll(
   collectionname As aPatch.
   Patch(
      tablename,
      {Id :aPatch.id},
      (
         name:aPatch.name,
         Age:aPatch.Age
      }
   )
)

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

Visit my blog Practical Power Apps

View solution in original post

12 REPLIES 12
WarrenBelz
Most Valuable Professional
Most Valuable Professional

Hi @Gelos ,

Assuming the Id fields line up

Clearcollect(
   collectionname,
   {
      id=1,
      name=geek,
      Age=20
      id=2,
      name=geek,
      Age=20
      id=3,
      name=geek,
      Age=20
   }
);
ForAll(
   collectionname As aPatch.
   Patch(
      tablename,
      {Id :aPatch.id},
      (
         name:aPatch.name,
         Age:aPatch.Age
      }
   )
)

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

Visit my blog Practical Power Apps

Gelos
Post Partisan
Post Partisan

Thank you for your replay. what mean apacth is it function or else.

WarrenBelz
Most Valuable Professional
Most Valuable Professional

@Gelos ,

Please read this document - it serves as an identifier that address potential ambiguity.

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

Visit my blog Practical Power Apps

Ok thank you. I will look at that. 

I tried it but it can not update. It will be inserted I want to be updated.

Thanks Warren. I changed this formula for update.

@Gelos 

Also, as a follow on - the ForAll is backward in your formula and not used properly.  ForAll returns a table of records, it is very inefficient as a ForLoop like in development (which PowerApps is not!).

 

If the columns of your collection have the primary key (Id) and the names match, then the only formula you need is:

Patch(tablename, collectionname)

What is nice about this as well, is that if a primary key is found, Patch will update it.  If a primary key is NOT found, then it will create a record.  So, you can use the above very efficiently to both update and create at the same time.

 

In the above, the entire table is given to patch all at once and Patch is instantiated once.  With a ForAll, the Patch is instantiated for each iteration of the ForAll table source, this will impact performance heavily.

 

If a more customized record is needed from a collection (i.e. the column names don't match), you can utilize the RenameColumns function to make them match and the DropColumn (or ShowColumns depending on the number of columns in your collection) to match the columns you want in your patch.

So, if your collection, for example, had a column called "PersonName" where the record in the datasource wanted a column called "name" and you also had a "Status" column in the collection, which does not exist in the datasource, then your formula would be:

Patch(tablename,
    DropColumns(
        RenameColumns(collectioname, "PersonName", "name"),
        "Status"
    )
)
             

Again, the above provides a table of records to the Patch function and it is only instantiated once.

 

If you need more customization, the ForAll is one of the most powerful functions in PowerApps (when used right)!  ForAll creates a table.  Then this can be used to provide to the Patch function.  Example, you want to change the name of all records and prepend the name with "Name:" 

Patch(tablename,
    ForAll(collectionname,
        {Id: Id
         name: "Name:" & name,
         Age: Age
        }
    )
)

This will provide a table to the patch function that will have that adjustment made.

 

To avoid all the typing of column names when modifying only one or a select few columns, this formula would do the exact same as the above formula:

Patch(tablename,
    ForAll(collectionname As _item,
        Patch(_item, {name: "Name:" & name})
    )
)

Again, all of the above are more performant as the Patch function is only instantiated once and provided a table of records that are all ready to go.

When the ForAll is backward, PowerApps has to repeat the same actions and instantiate the Patch function for each iteration of the ForAll table.  While it might work, performance will suffer and, as you can see, you will type much more formula to have to maintain.

 

Once you view the ForAll as the function it was made to be, you will find it to be the most powerful function in PowerApps!  Looking at it as a ForLoop and you will curse at it for what it is not.

 

I hope this is informative and 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!

Hi @RandyHayes ,

Nice to hear from you. I have seen a couple of posts from you on this subject before and understand the performance advantages of a single Patch with one write to the data source, however I have not managed to get it to work (if only in a couple of attempts), In the example, the code

ClearCollect(
   collectionname,
   {
      id: 1,
      name: "geek",
      Age: 20
   },
   {
      id: 2,
      name: "geek",
      Age: 20
   },
   {
      id: 3,
      name: "geek",
      Age: 20
   }
);
ClearCollect(
   tablename,
   {
      id: 1,
      Name: "geek",
      Age: 20
   },
   {
      id: 2,
      Name: "geek",
      Age: 20
   },
   {
      id: 3,
      Name: "geek",
      Age: 20
   }
);
ForAll(
   collectionname As aPatch,
   Patch(
      tablename,
      {id: aPatch.id},
      {
         Name: aPatch.name,
         Age: aPatch.Age
      }
   )
);

works as expected, however when I replace the bottom bit with your example

Patch(
   tablename,
   ForAll(
      collectionname As _item,
      Patch(
         _item, 
         {name: "name:" & name}
      )
   )
)

I get the error below

 

WarrenBelz_0-1641677480419.png

@WarrenBelz 

Good to hear from you too!  Hope all is well and you are enjoying the New Year!!

 

Yes, what you are seeing is Patch "throwing a fit" because in that example you are trying to Patch a collection.  A collection is just a table, so it does not have things that a datasource does...like a Primary Key.  This is a disadvantage for collections (one of many) as Patch will only treat them one way.  So with a Patch function on a collection, you need to always specify the entire record to be Patched  - Patch(collection, <record>, <record changes>

 

When patch is used with a datasource, it is primarily (no pun intended) working with the Primary Key for everything.  So patch will determine what to do based on the primary key - if there, update.  If not, create.  And it will take a full table of all the records to update or create all at the same time.

 

I used to think the only way to do a remove on records (in a delegable way) was with the RemoveIf function.  And since it only took one criteria, I thought this was one place that I needed to reverse a ForAll into a ForLoop.

i.e.:   

ForAll(tableOfRecords As removes,
   RemoveIf(datasource, ID=removes.ID)
)

This was horrible for performance on large lists.

 

Then I looked closely at the docs and took note that there were TWO syntaxes for Remove:

RandyHayes_0-1641678595517.png

 

This made me change the formula back to a normal table:

Remove(datasource, tableOfRecords)

In this formula, Remove (just like Patch) looks at the primary ID in the tableOfRecords table.  It will then (and this is Delegable) remove the records by their primary key.

So, there was no longer a need for a ForAll, it was just that simple.

 

Now, Remove is smart enough to work with collections as well, but that matching is ENTIRE record.  So if I did this on a datasource:

Remove(datasource, tableOfRecords.ID)

The above would only be providing a table with an ID column to the Remove function.  And it works fine.

But:

Remove(colletion, tableOfRecords.ID)

Would not work because it expects the entire record of the collection to match it (as there is no primary key)

 

 

One other thing about that error message you showed.  That error is confusing in one other place where it shows.  

If you use a Patch(dataSource, collection) statement, sometimes you will see that same message.  It is misleading because what it really means is that there is a column in your collection that is NOT in the datasource, or that a column is not named properly in the collection to match the datasource (and that match needs to be "real" column names - as in the case of renaming a SharePoint column...the collection needs to contain the original column name.)  When I first saw that scenario, I pulled some hair out for sure until I understood that it was a misleading message!

 

 

_____________________________________________________________________________________
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!

Helpful resources

Announcements

Community will be READ ONLY July 16th, 5p PDT -July 22nd

Dear Community Members,   We'd like to let you know of an upcoming change to the community platform: starting July 16th, the platform will transition to a READ ONLY mode until July 22nd.   During this period, members will not be able to Kudo, Comment, or Reply to any posts.   On July 22nd, please be on the lookout for a message sent to the email address registered on your community profile. This email is crucial as it will contain your unique code and link to register for the new platform encompassing all of the communities.   What to Expect in the New Community: A more unified experience where all products, including Power Apps, Power Automate, Copilot Studio, and Power Pages, will be accessible from one community.Community Blogs that you can syndicate and link to for automatic updates. We appreciate your understanding and cooperation during this transition. Stay tuned for the exciting new features and a seamless community experience ahead!

Summer of Solutions | Week 4 Results | Winners will be posted on July 24th

We are excited to announce the Summer of Solutions Challenge!   This challenge is kicking off on Monday, June 17th and will run for (4) weeks.  The challenge is open to all Power Platform (Power Apps, Power Automate, Copilot Studio & Power Pages) community members. We invite you to participate in a quest to provide solutions in the Forums to as many questions as you can. Answers can be provided in all the communities.    Entry Period: This Challenge will consist of four weekly Entry Periods as follows (each an “Entry Period”)   - 12:00 a.m. PT on June 17, 2024 – 11:59 p.m. PT on June 23, 2024 - 12:00 a.m. PT on June 24, 2024 – 11:59 p.m. PT on June 30, 2024 - 12:00 a.m. PT on July 1, 2024 – 11:59 p.m. PT on July 7, 2024 - 12:00 a.m. PT on July 8, 2024 – 11:59 p.m. PT on July 14, 2024   Entries will be eligible for the Entry Period in which they are received and will not carryover to subsequent weekly entry periods.  You must enter into each weekly Entry Period separately.   How to Enter: We invite you to participate in a quest to provide "Accepted Solutions" to as many questions as you can. Answers can be provided in all the communities. Users must provide a solution which can be an “Accepted Solution” in the Forums in all of the communities and there are no limits to the number of “Accepted Solutions” that a member can provide for entries in this challenge, but each entry must be substantially unique and different.    Winner Selection and Prizes: At the end of each week, we will list the top ten (10) Community users which will consist of: 5 Community Members & 5 Super Users and they will advance to the final drawing. We will post each week in the News & Announcements the top 10 Solution providers.  At the end of the challenge, we will add all of the top 10 weekly names and enter them into a random drawing.  Then we will randomly select ten (10) winners (5 Community Members & 5 Super Users) from among all eligible entrants received across all weekly Entry Periods to receive the prize listed below. If a winner declines, we will draw again at random for the next winner.  A user will only be able to win once overall. If they are drawn multiple times, another user will be drawn at random.  Individuals will be contacted before the announcement with the opportunity to claim or deny the prize.  Once all of the winners have been notified, we will post in the News & Announcements of each community with the list of winners.   Each winner will receive one (1) Pass to the Power Platform Conference in Las Vegas, Sep. 18-20, 2024 ($1800 value). NOTE: Prize is for conference attendance only and any other costs such as airfare, lodging, transportation, and food are the sole responsibility of the winner. Tickets are not transferable to any other party or to next year’s event.   ** PLEASE SEE THE ATTACHED RULES for this CHALLENGE**   Week 1 Results: Congratulations to the Week 1 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Community MembersNumber of SolutionsSuper UsersNumber of Solutions @anandm08  23 @WarrenBelz  31 @DBO_DV  10 @Amik  19 AmínAA 6 @mmbr1606  12 @rzuber  4 @happyume  7 @Giraldoj  3@ANB 6 (tie)   @SpongYe  6 (tie)     Week 2 Results: Congratulations to the Week 2 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Community MembersSolutionsSuper UsersSolutions @anandm08  10@WarrenBelz 25 @DBO_DV  6@mmbr1606 14 @AmínAA 4 @Amik  12 @royg  3 @ANB  10 @AllanDeCastro  2 @SunilPashikanti  5 @Michaelfp  2 @FLMike  5 @eduardo_izzo  2   Meekou 2   @rzuber  2   @Velegandla  2     @PowerPlatform-P  2   @Micaiah  2     Week 3 Results: Congratulations to the Week 3 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Week 3:Community MembersSolutionsSuper UsersSolutionsPower Apps anandm0861WarrenBelz86DBO_DV25Amik66Michaelfp13mmbr160647Giraldoj13FLMike31AmínAA13SpongYe27     Week 4 Results: Congratulations to the Week 4 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Week 4:Community MembersSolutionsSuper UsersSolutionsPower Apps DBO-DV21WarranBelz26Giraldoj7mmbr160618Muzammmil_0695067Amik14samfawzi_acml6FLMike12tzuber6ANB8   SunilPashikanti8

Check Out | 2024 Release Wave 2 Plans for Microsoft Dynamics 365 and Microsoft Power Platform

On July 16, 2024, we published the 2024 release wave 2 plans for Microsoft Dynamics 365 and Microsoft Power Platform. These plans are a compilation of the new capabilities planned to be released between October 2024 to March 2025. This release introduces a wealth of new features designed to enhance customer understanding and improve overall user experience, showcasing our dedication to driving digital transformation for our customers and partners.    The upcoming wave is centered around utilizing advanced AI and Microsoft Copilot technologies to enhance user productivity and streamline operations across diverse business applications. These enhancements include intelligent automation, AI-powered insights, and immersive user experiences that are designed to break down barriers between data, insights, and individuals. Watch a summary of the release highlights.    Discover the latest features that empower organizations to operate more efficiently and adaptively. From AI-driven sales insights and customer service enhancements to predictive analytics in supply chain management and autonomous financial processes, the new capabilities enable businesses to proactively address challenges and capitalize on opportunities.    

Updates to Transitions in the Power Platform Communities

We're embarking on a journey to enhance your experience by transitioning to a new community platform. Our team has been diligently working to create a fresh community site, leveraging the very Dynamics 365 and Power Platform tools our community advocates for.  We started this journey with transitioning Copilot Studio forums and blogs in June. The move marks the beginning of a new chapter, and we're eager for you to be a part of it. The rest of the Power Platform product sites will be moving over this summer.   Stay tuned for more updates as we get closer to the launch. We can't wait to welcome you to our new community space, designed with you in mind. Let's connect, learn, and grow together.   Here's to new beginnings and endless possibilities!   If you have any questions, observations or concerns throughout this process please go to https://aka.ms/PPCommSupport.   To stay up to date on the latest details of this migration and other important Community updates subscribe to our News and Announcements forums: Copilot Studio, Power Apps, Power Automate, Power Pages

Users online (926)