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 / Is there a better way ...
Power Apps
Unanswered

Is there a better way to patch multiple changes to a data table at once than this?

(0) ShareShare
ReportReport
Posted on by 10
I have an editable grid of items that looks like this: 

These are test fields that I've created just to figure out this patching method and all of these data cells appropriately patch. The New button adds a new record to the dataverse data table with a patch command, then the Save button saves any fields that are changed. The column with TE is the ID column

So here's where I'm at:

- At first, every editable element in my application sent a patch command when the OnChange property is invoked WITHOUT using the save command, but that creates a problem where if you are editing cells too fast, some of your changes will be undone during the patch. 

- I pivoted to collecting all changes in a collection, then patching the collection to the database with a manual save. However, this creates a problem where instead of updating the existing data table entries, it adds each column item as a new one, leaving the unedited ones as duplicates with the same ID as the changed entry.

My current solution patches the collection, but deletes any duplicate records that come from it. Here is my code:

Whenever a cell is edited, it selects a hidden button3. When button3 is selected, this code runs: 
 
 
If(
    IsBlank(
        LookUp(
            colGridUpdates,
            ID = ThisItem.ID
        );
    ),
    Collect(
        colGridUpdates,
        ThisItem
    );
    Notify("Record collected. Item ID: " & ThisItem.ID, NotificationType.Success,3000);
);
UpdateIf(colGridUpdates, ID = ThisItem.ID, {Value: TextInput2.Text, Number: Dropdown1_1.Selected.Value, Bool: Checkbox2.Value, Dropdown: Dropdown1.Selected.Value})
//This adds an item to a collection of changes when it has been edited, or updates any collection items that are already there. 
 
 
This is the code in the save button:
 

ForAll(
    colGridUpdates, 
    Collect(DupeDeleteCol, Distinct(colGridUpdates, ID))
);

ForAll(
    colGridUpdates, 
    If(ID in DupeDeleteCol, Remove(TestTables,ThisRecord))
);

Patch(TestTables, ShowColumns(colGridUpdates, ID, Value, Number, Bool, Dropdown));
Notify("Changes saved.", NotificationType.Success,3000);
Clear(colGridUpdates);
Clear(DupeDeleteCol);
Set(varReset, false);
Set(varReset, true);
// This collects the ID of all items in the collection of changes. Then, for all items in that duplicate ID collection, it deletes items from the database that share the same ID, because the patch command ends up creating duplicate records. Then the two collections are cleared.

This does work, however I am worried that if the patch command ever fails but the remove command still goes through, it will result in data loss. 

Is there a better way to patch multiple changes to a database at once? Am I doing the collection patch wrong? Or is there a way to keep this current structure but add a certain form of error checking that will not push the delete command if the patch command does not work? 
Categories:
I have the same question (0)
  • WarrenBelz Profile Picture
    153,079 Most Valuable Professional on at
    If you want to update TestTables from matching records in colGridUpdates, but create new records if a matching ID does not exist (I assume you are using SharePoint here)
    Patch(
       TestTables,
       ForAll(
          colGridUpdates As _Data,
          With(
             {
                _ID:
                LookUp(
                   TestTables,
                   ID = _Data.ID
                ).ID
             },
             {
                ID: _Data.ID,
                Value: _Data.Value, 
                Number: _Data.Number, 
                Bool: _Data.Bool,
                Dropdown: _Data.Dropdown
             }
          )
       )
    )
     
    Please click Does this answer your question 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 a Like.
    MVP (Business Applications)     Visit my blog Practical Power Apps    Buy me a coffee
  • PD-22081400-0 Profile Picture
    10 on at
    @WarrenBelz I am using Dataverse, but I will see if this works.
  • WarrenBelz Profile Picture
    153,079 Most Valuable Professional on at
    SharePoint has a strength here in automatically creating a new record if the ID does not exist or updating the existing item if it does. I am not sure on Dataverse (I do not use it), but you would need to use the unique identifier.
  • Wee Teck Profile Picture
    45 on at
    • For the Save Button:

      • Use a ForAll() loop to patch each record individually and capture the result.
      • Only delete records if the patch for that record was successful.
      • Notify the user if any patches fail.
    • Error Handling via Errors Function: You can use the Errors function to detect whether the patch command has failed. If it fails, don't proceed with deletion.

    Reference:

    ForAll(
        colGridUpdates, 
        With(
            {
                result: Patch(TestTables, LookUp(TestTables, ID = ThisRecord.ID), ThisRecord)
            },
            If(
                IsEmpty(Errors(TestTables, result)),
                Remove(TestTables, LookUp(TestTables, ID = ThisRecord.ID)),
                Notify("Patch failed for record with ID: " & ThisRecord.ID, NotificationType.Error, 3000)
            )
        )
    );
    Clear(colGridUpdates);
    Notify("Changes saved successfully.", NotificationType.Success, 3000);
     

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 721 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 320 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard