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

Patch not working to Update a record in a local collection

Hey Guys, Im having issues when I'm trying to update a record in a local collection 'MileageCache' using the patch function. The strange thing is the code works fine updating the sharepoint list directly.

 

I am wanting to update a local collection of the sharepoint list so adding records works fine when offline.

 

Here is the code im using, like I say it works fine with the online sharepoint list directly:

 

    UpdateContext(
        {
            tempMileageItem: First(
                Filter(
                    MileageCache,
                    ID = MileageItem.ID
                )
            )
        }
    );

    Patch(
        MileageCache,
        tempMileageItem,
        Patch(
            tempMileageItem,
            {
                'End Mileage': Value(MileageText.Text),
                'End Date': Now(),
                'End Location': Location.Latitude & "," & Location.Longitude
            }
        )
    )

The global variable MileageItem is a single record from MileageCache and is set by:

 

    Set(
        MileageItem,
        Patch(
            MileageCache,
            Defaults(MileageCache),
            {
                'ID':100,
                'Start Mileage': Value(MileageText.Text),
                'Start Date': Now(),
                'Start Location': Location.Latitude & "," & Location.Longitude
            }
        )
    )

Does anyone have any ideas why I am unable to update the record in the collection using the Patch function ?

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Patch not working to Update a record in a local collection

Hi @henda79 

The Patch function can be buggy, especially when local collections and null values are involved.

Here's an example of what I mean.

https://powerusers.microsoft.com/t5/General-Discussion/Patch-not-updating-on-null-values-in-database...

If you just want to update a record in your MileageCache collection, you could consider the use of the UpdateIf function rather than Patch, as from my experience, this tends to work more reliably in these types of circumstances.

4 REPLIES 4
Super User
Super User

Re: Patch not working to Update a record in a local collection

Hi @henda79 

The Patch function can be buggy, especially when local collections and null values are involved.

Here's an example of what I mean.

https://powerusers.microsoft.com/t5/General-Discussion/Patch-not-updating-on-null-values-in-database...

If you just want to update a record in your MileageCache collection, you could consider the use of the UpdateIf function rather than Patch, as from my experience, this tends to work more reliably in these types of circumstances.

henda79
Level: Powered On

Re: Patch not working to Update a record in a local collection

Ok, I'll give it a whirl and let you know.

 

I thought its was a issue with Patch as I had spent several hours trying to "debug" the issue, but since you can't debug, issues like this take forever to work out.

 

Thanks.

henda79
Level: Powered On

Re: Patch not working to Update a record in a local collection

thanks @timl , using UpdateIf rather than Patch works fine.

It seems that Patch works OK to insert the initial record but it seems to fail for the update with a collection but not for an online source.

Super User
Super User

Re: Patch not working to Update a record in a local collection

Great! I'm glad that UpdateIf solved your problem

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: 245 members 4,311 guests
Please welcome our newest community members: