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?