powerappspowerapps-canvaspowerapps-collection

Duplicate records being added unexpectedly to my collection in Powerapps?


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!


Solution

  • 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.