cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
MikeAnderson
Level 8

Patch creating duplicate record

Hello,

I'm having an issue with Patch() creating duplicate records in my collection.  My goal is to update a few fields in a record within a collection and then Patch the collection to the database to update the fields in the database.  The issue I'm running into is that Patch is creating a duplicate record in the collection.  I have a gallery, where a user selects a record and can delete the record.  The delete is a soft delete in that a field in the record contains an IsDeleted value. Here is the collection before modification:

1.png

Here I've selected the record I wish to delete and verfied that the selected record is correct by using a label to display the PK ID of the record.

2.png

 

Here is the orignal collection now showing a duplicate value:

3.png

 

Finally, here are the various permutations of Patch I've tried to make this successfully work:
Using Lookup:

Patch(PendingEquipmentRequestsCollection, Defaults(PendingEquipmentRequestsCollection),
    LookUp(PendingEquipmentRequestsCollection, ID = PendingEquipmentGallery.Selected.ID),
    {IsDeleted: true, ModifiedDate: Now(), ModifiedBy:varCurrentUser.Email}
);

Using First/Filter:

Patch(PendingEquipmentRequestsCollection, Defaults(PendingEquipmentRequestsCollection),
    First(Filter(PendingEquipmentRequestsCollection, ID = PendingEquipmentGallery.Selected.ID)),    
        {IsDeleted: true, ModifiedDate: Now(), ModifiedBy:varCurrentUser.Email}
);

Here is the validation of the selected record displayed in the label box:

First(Filter(PendingEquipmentRequestsCollection, ID = PendingEquipmentGallery.Selected.ID)).ID

If I don't include 

Defaults(PendingEquipmentRequestsCollection),

Nothing happens at all, but when I include it, it duplicates. Any suggestions on what I'm doing wrong?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Dual Super User
Dual Super User

Re: Patch creating duplicate record

YOu can't use Patch to update existing records in a collection.  For that you need to use Update().  Patch can only be used to create new records in a collection.  It can update records in a datasource, but not a collection.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

View solution in original post

3 REPLIES 3
Dual Super User
Dual Super User

Re: Patch creating duplicate record

YOu can't use Patch to update existing records in a collection.  For that you need to use Update().  Patch can only be used to create new records in a collection.  It can update records in a datasource, but not a collection.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

View solution in original post

lucia
Level 8

Re: Patch creating duplicate record

Hey,

 

Defaults() will automatically create new item. You just have to try using it without it and be sure that the ID validation is correct.

 

Patch(DataSource, {ID : MyID}, {field: value})

Hope this helps.

lucia
Level 8

Re: Patch creating duplicate record

Also you should not use, I think, modified date and by who it was modifaied as this happens automatically in Sharepoint.

Helpful resources

Announcements
thirdimage

Power Apps Super User Class of 2020

Check it out!

thirdimage

New Badges

Check it out!

thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

SecondImage

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Solution Authors
Top Kudoed Authors
Users online (5,594)