0

I am very new to PowerApps so hopefully my question is clear. I have a collection that I created called colGridData which patches new records that I create in colGridData to an online Sharepoint list. In general, this works great when the records already exist on the Sharepoint list (i.e., editing the records seems to work fine). However, when I attempt to add a record to the collection or delete a record from a collection, duplicate records get created. The unusual thing is that they don't always get created, but seemingly more at random. For example, if some cases I can add a record just fine and then in other instances I add or delete a record and multiple existing records get duplicated in the collection. Ultimately, since the records never get loaded to the Sharepoint the duplicate records disappear as I add new records to the collection or otherwise toggle within my application. One guess is that the colGridData may be updating/refreshing too quickly before the changes "take". I have set my text fields to delay update = true.

Here is the code I use when adding a record to the collection:

Set(varNumber,varNumber+1);

Collect(
    colGridData,
    Patch(varNewRecord,{ID:varNumber}
    )
)

And then after the record is added and the fields are updated, I exit the colGridData with the following:

If(
    varGridEdit,
    Patch(
        Nominations,
        UpdateIf(colGridData,Created = Blank(),{ID:Blank()}
        )
    )
);

Remove(Nominations,colDelete);
Clear(colDelete);

Select(btnLoadData);    
Set(varGridEdit,!varGridEdit);

Finally, here is the code in btnLoadData:

ClearCollect(
    colGridData,
    Filter(
        Nominations,
        StartsWith(Title,txtEmployeeID.Text)
        && StartsWith('Preferred Name',txtName.Text)
        && StartsWith(Status.Value,txtStatus.Text)
    )
);

When an erroneous record gets added, it seems that the ID is blank and the "Created" value is also blank. Does anyone know a way to stop the erroneous records from being added? Do I need to add some code to "stop" the creation of additional blank IDs? Any guidance you can provide will be much appreciated as I learn to work within PowerApps. Thank you!

Chris2015
  • 1,030
  • 7
  • 28
  • 42
  • Since you included Select(btnLoadData) in your code, it could be a reason for the random behaviour of your app. It executes the OnSelect code of btnLoadData asynchronously. Can you clarify what this button does? – mmikesy90 Apr 12 '23 at 06:43
  • Also, what are you trying to accomplish with this: UpdateIf(colGridData,Created = Blank(),{ID:Blank()}) – mmikesy90 Apr 12 '23 at 06:44
  • Thanks for your response. The (btnLoadData) code clears the collection and allows for filtering the collection (colGridData) via a search. I've added this code to my original question. For (UpdateIf) I am isolating the newly added record to the collection and working to assign a "dummy" ID value. – Chris2015 Apr 13 '23 at 14:01
  • After removing the add item capability, I think part of my problem may have been incorrect field references on the (Patch) which I have since corrected. However, I still see duplicates created when I select via a drop-down (i.e., I make a selection and it duplicates the selection across subsequent rows in my gallery). I wonder if some of these problems are the result of timing - making too many selections too quickly for the app and Sharepoint to "catch up". Have you seen this type of behavior in any apps? – Chris2015 Apr 13 '23 at 14:02

1 Answers1

0

It's not entirely clear to me why you are using colGridData. I guess you wanted to store the updates before flushing it all to SharePoint. However this design causes that two sets of the same rows is loaded into the memory of the user's device. Including the rows you are not updating.

I strongly recommend that instead of the collection you bind Nominations directly to the Gallery.Items property. A button then could directly create an empty record on SharePoint, which appears in your gallery automatically. Users can finish editing the new row's details the same way they edit the rest. It is up to you if your saving edits logic is implemented individually for every row or patching a colUpdates collection first and then bulk update SP.

This approach also saves you from the pain of messing with ID and Created.

mmikesy90
  • 783
  • 1
  • 4
  • 11
  • Thank you! Yes, the colGridData is meant to apply all the updates to SharePoint in a single go to optimize performance. I will explore the approach you've outlined and see if I can get it to work in my application. – Chris2015 Apr 19 '23 at 16:39