cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MikeAnderson
Level: Powered On

Patch creating duplicate records

Hello,

 

I'm attempting to update a few fields for a record in a collection using Patch(). The code I have is duplicating the record rather than updating it.  My goal is to update the collection, and once updated, use Patch() to then update the records and fields in the database. At this point however, I can't even get the collection updated and I'm not sure why?

I've tried the following methods of using PATCH() and both methods result in a duplicate record being created.

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

If I omit Defaults(PendingEquipmentRequestsCollection) in Patch, nothing happens (no duplicate, but also, no edit of the existing fields).  If I include Defaults(PendingEquipmentRequestsCollection) in Patch then the record is duplicated.  I validated that the Filter / Lookup is returning the correct ID by place this in a label:

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

Here is the collection before modification:
1.png

Here is the form showing the correct ID is selected (which is the PK):
2.png

After clicking the trash can icon for the selected item, here is the duplicate record appearing in the collection:

3.png

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Patch creating duplicate records

Ok, I think I see what is happening.  Patch is used to update or create records in a datasource, not in a collection. So when you use the defaults parameter what i think is happening is that a new record is being created in the datasource itself and then reflected in the collection.  But you can't use patch the same way with a collection.  For collections it can only be used to add records to the collection, not change existing ones. To update existing records in the collection you use Update and specify the full old record and the new record.

https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-update-updateif

Be aware that this won't change records in the datasource.  For that you still need to run the patch.

 

Sorry I hadn't picked up that this was running against a collection and not a datasource.  If you look at the intellisense when using Patch you will see that it changes when you insert the name of a collection in place of an actual datasource.



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

Re: Patch creating duplicate records

The way Patch works if you supply the defaults(listname) as the second parameter it always creates a new record.  For updating existing records you need to supply the record to be updated as the second parameter of the patch statement.  So it looks like your first formula is basically correct if you remove the defaults argument and let the First() entry become the argument that identifies the record to be patched.



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

Re: Patch creating duplicate records

Thank you for the response and explaining the defaults.  The issue for me is that without defaults (which I added to see if I could get a duplicate) the record is not updated.  So I'm stumped why it can duplicate the record, but not update the existing one. Any thoughts on that? Or how I might catch what error might be occuring, which is odd that since it duplicates there shouldn't be an error I wouldn't think?

Super User
Super User

Re: Patch creating duplicate records

AS I said, when using Patch to update existing records you need to supply the record to be updated where the defaults parameter goes for a new record.  For example,something like this

Patch( Datasource, First( Filter( Datasource, Name = "unique name" ) ), { fieldname: “Field updated value”, fieldname2: "field2 updated value" } )

If its not updating the existing record then make sure that whatever you have in the second parameter returns the actual record you want updated.  In this case it would be the First(...) function.



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

Re: Patch creating duplicate records

My code DOES contain the record to be updated as noted here and in the previous code example:

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

 I also tried a lookup to indicate which record:

  LookUp(PendingEquipmentRequestsCollection, ID = PendingEquipmentGallery.Selected.ID),

Unless my filtering is wrong, this should identify the record to update as the ID is unique to the record in the collection.  In addition, if I patch directly against the datasource, it updates just fine, which I take as my code is filtering correctly. 

This works directly against the DATABASE:

Patch(
 '[dbo].[EquipmentRequests]',  //This is the azure sql db data source
    First(
        Filter(
            PendingEquipmentRequestsCollection,
            ID = PendingEquipmentGallery.Selected.ID
        )
    ),
    {
        IsDeleted: true,
        ModifiedDate: Now(),
        ModifiedBy: varCurrentUser.Email
    }
);

DatasourceSave.png


Yet not against the COLLECTION (only 1 field is updated):

Patch(
   PendingEquipmentRequestsCollection,   //This is the collection
    First(
        Filter(
            PendingEquipmentRequestsCollection,
            ID = PendingEquipmentGallery.Selected.ID
        )
    ),
    {
        IsDeleted: true,
        ModifiedDate: Now(),
        ModifiedBy: varCurrentUser.Email
    }
);

 AfterSave.png

Super User
Super User

Re: Patch creating duplicate records

Ok, I think I see what is happening.  Patch is used to update or create records in a datasource, not in a collection. So when you use the defaults parameter what i think is happening is that a new record is being created in the datasource itself and then reflected in the collection.  But you can't use patch the same way with a collection.  For collections it can only be used to add records to the collection, not change existing ones. To update existing records in the collection you use Update and specify the full old record and the new record.

https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-update-updateif

Be aware that this won't change records in the datasource.  For that you still need to run the patch.

 

Sorry I hadn't picked up that this was running against a collection and not a datasource.  If you look at the intellisense when using Patch you will see that it changes when you insert the name of a collection in place of an actual datasource.



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

Re: Patch creating duplicate records

Thank you! I didn't understand from the documenation of Patch that it wouldn't work against a collection. Interestingly it works against a single field though.  I changed from Patch to Update and the collection updated as expected.

Super User
Super User

Re: Patch creating duplicate records

It caught me when I was first learning to use Patch too. Sorry I didn't notice that you were using a collection.  We could have gotten to the answer a lot quicker.



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

Re: Patch creating duplicate records

No apologies needed! You solved my mystery and gave me the answer I needed, much appreciated!

Helpful resources

Announcements
firstImage

PowerApps Monthly Community Call!

Join us next Wednesday for our Demo Extravaganza, October 16, 2019 8am PDT.

firstImage

Microsoft Business Applications Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

FirstImage

Power Platform World Tour

Coming to a city near you

thirdimage

PowerApps Community User Group Member Badge

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

FourthImage

Join PowerApps User Group!!

Connect, share, and learn with your peers year-round

SecondImage

Power Platform Summit North America

Register by September 5 to save $200

Top Kudoed Authors
Users Online
Currently online: 269 members 5,037 guests
Please welcome our newest community members: