web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Patch a Collection wit...
Power Apps
Answered

Patch a Collection with LookUp to update existing records

(0) ShareShare
ReportReport
Posted on by 214

Hi all, so I have  a collection that is being created based on modified fields, so I want to push this changes back to the source, but for some it is not working.

 

If I remove the Lookup in there and just leave the source (CurrentR) and the collection(crUpdates), it will create new records. Collection is made of only 3 fields to edit and also the id of the excel table (which is just a unique row number).

 

patch col.png

Categories:
I have the same question (0)
  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @Joorge_c 

    If your collection (assuming crUpdates) is a match to the datasource (assuming CurrentR), then please consider changing your Formula to the following:

        Collect(CurrentR, crUpdates)

     

    PowerApps will do the rest of matching, patching, and creating if needed.

     

    I hope this is helpful for you.

  • Joorge_c Profile Picture
    214 on at

    Thanks @RandyHayes , so If I run the next steps, I noticed nothing gets saved or pushed back to the Excel Table, I realized due to the Excel keeping the same updated timeline on last modified.

     

    Onstart: 

    ClearCollect(crUpdates,  CurrentR);  Clear(crUpdates)
     
    Onchange:
    CollectcrUpdatesThisItem )
     
    Button:
    Patch(CurrentR, crUpdates );
  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @Joorge_c 

    Yes, patch will not work...the formula I provided you was:

     Collect(CurrentR, crUpdates)

     

    I'm not sure why you are Collecting all the records of CurrentR and then clearing them all from your collection in the OnStart??  But, that is not the issue, just a curiosity.

     

  • Joorge_c Profile Picture
    214 on at

    Hi @RandyHayes Thank you for your follow up and help.

    (OptionB) Collecting and clearing them at start, gives me only all the collumn names.

     

    So this was my initial attemp (OptionA) considering only the 3 fields I want to update, in which new records were created by mistake in the patch.

    Any ideas on how I can get the below to update and not create new records, with all columns in blank and only the 3 fields populated with the modified values?

     

    On Start:

    ClearCollect(
    crUpdates,
    ShowColumns(
    Table(Defaults(CurrentR)),
    "Renewing_x003f_",
    "Renewal_x0020_Month",
    "New_x0020_Contract_x0020_Start_x0020_Date",
    "id"
    );
    );
    Clear(crUpdates)
     
     

    On Change:

    If(ThisItem.id in crUpdates.id,
    Update(crUpdates,
    LookUp(crUpdates,id=ThisItem.id),
    {
    id: ThisItem.id,
    Renewing_x003f_: 'CR_Renewing?_Drop'.Selected.Value,
    Renewal_x0020_Month: CR_RenewalMonth_Drop.Selected.Value,
    New_x0020_Contract_x0020_Start_x0020_Date: CR_ContractSDate_Drop.Selected.Value
    }),

    Collect(
    crUpdates,
    {
    id: ThisItem.id,
    Renewing_x003f_: 'CR_Renewing?_Drop'.Selected.Value,
    Renewal_x0020_Month: CR_RenewalMonth_Drop.Selected.Value,
    New_x0020_Contract_x0020_Start_x0020_Date: CR_ContractSDate_Drop.Selected.Value
    }
    ))
     
     
     

    Submit Button:

    If(
    CountRows(crUpdates)>0,
    Patch(
    CurrentR, crUpdates
    );
    Notify(
    "Success",
    NotificationType.Success
    )
    );
    Clear(crUpdates);

     

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @Joorge_c 

    Get rid of the formula you have in the OnStart, you're not going to need that.

     

    So, for your OnChange: 

    RemoveIf(crUpdates, id=ThisItem.id);
    Collect(crUpdates,
     {
     id: ThisItem.id,
     Renewing_x003f_: 'CR_Renewing?_Drop'.Selected.Value,
     Renewal_x0020_Month: CR_RenewalMonth_Drop.Selected.Value,
     New_x0020_Contract_x0020_Start_x0020_Date: CR_ContractSDate_Drop.Selected.Value
     }
    )

    This removes the item from the collection if it exists.  Then it adds the current record.

     

    For your Submit:

    With({_results:
     ForAll(crUpdates As _update,
     Patch(CurrentR,
     Coalesce(LookUp(CurrentR, id=_update.id), Defaults(CurrentR)),
     _update
     )
     );
     
     If(IsEmpty(Errors(CurrentR)), 
     Notify("Success", NotificationType.Success)
     )
    );
    
    Clear(crUpdates);

    Since you are dealing with updating records, we can't just push them back with the Collect (collect to a datasource will add new records, but it will not update existing).  So we do look to our friend the Patch function for this.

     

    This is somewhat along the lines of where you were going.  I didn't add the extra error checking in here because you are dealing with Excel and, since I don't work with it a lot as a datasource, I'm not sure that any record by record errors are relevant, but I left the general construct in.

    So, we ForAll on the collection and then either create a record or update an existing record (Coalesce helps us with that).

    Finally, we just check the errors on the datasource to determine if the notify happens.

     

    Hopefully that will move you to the next step.

     

  • Joorge_c Profile Picture
    214 on at

    Thanks @RandyHayes  I feel like Im almost there. Getting an Invalid number of Arguments in the Submit portion. I feel it has to do with the CurlyClose (from _results), I tried a couple of way to fix it but Im a lil confused.

  • Verified answer
    RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @Joorge_c 

    Good call...I typo'ed.

    Here is the revised:

    ForAll(crUpdates As _update,
     Patch(CurrentR,
     Coalesce(LookUp(CurrentR, id=_update.id), Defaults(CurrentR)),
     _update
     )
     );
     
    If(IsEmpty(Errors(CurrentR)), 
     Notify("Success", NotificationType.Success)
    );
    
    Clear(crUpdates);

    And actually, as I looked at this scenario and what we're going for, we really don't need the output of the ForAll...so we can let it fall into the eternal bit-bucket.  

    Normally I would capture that output and use it against any error checking.  But I believe for the purposes of what you are doing, a general error check is sufficient.

     

    See where that gets you.

     

  • Community Power Platform Member Profile Picture
    on at

    Thanks Randy--easy to follow and it works!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 739 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 343 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard