Skip to main content

Notifications

Community site session details

Community site session details

Session Id : 7bFFT6TN2GUxq2OGWzTiQf
Power Apps - Building Power Apps
Answered

Using the Patch function to either create OR update a record

Like (3) ShareShare
ReportReport
Posted on 20 Aug 2017 20:08:28 by 46

Hi, we are developing a free system that allows to coordinate the efforts of volunteer rescue and emergency groups working in a disaster or conflict zone. For us it's extremely important to be able to know where our medics and support teams are located. Since they use a PowerApps to create a report for every patient they attend, we incorporated into this same app a formula that is triggered with the OnVisible event and that sends us their location so it can be stored in a SharePoint list and then shown in a Power BI map. This is the formula to report their location  

 

Patch('SharepointList',Defaults('SharepointList'), {Title:"Title", Latitud:Location.Latitude,Longitud:Location.Longitude})

 

This formula is working great, however it creates a separate record every time. We would like to alter this formula to evaluate if the current user already has a location record in the list or not, and to create a record if it doesn't already have one or modify it if a record for this user already exists. This way we would only have the most update location for the user.

 

Any help with this would be greatly appreciated.

 

Best regards.

 

 

  • Newbie12 Profile Picture
    227 on 09 Oct 2020 at 16:51:44
    Re: Using the Patch function to either create OR update a record

    Hi @Meneghino,

     

    How will the above code work in Excel table and Textbox as fields in Edit form ?

  • MH3 Profile Picture
    1,057 on 15 Sep 2020 at 05:55:54
    Re: Using the Patch function to either create OR update a record

    Hi All,
    @gcanelon  @Meneghino 
    I have a Leave Application, in which I am using two lists.

    List 1) LeaveRequests

    List 2) EmployeeLeaveBalance.

    When the user comes in my app, it requests for Leave, Let's say Annual Leave and fill out the information in the form and press on the submit button,
    the Submit button The First Patch Command  first patches in the LeaveRequests List of that User, which stores (User Email, LeaveType, Description, Manager Email, Start Date and End Date). and after that another Patch Command runs which, patches that user's Leave Balance in the EmployeeLeaveBalance List which Stores (User Email, Total Leaves, Annual Leave balance and Days Requested).

    Now, I want to save only 1 record for each employee and then if he/she requests again. So, I want to update the EmployeeLeaveBalance List and update their LeaveBalance Column Everytime which is Subtracted from their Days Requested.

    how to do this, Any help in this Regard.

    Thanks!!! 

  • Community Power Platform Member Profile Picture
    on 03 Aug 2020 at 13:56:05
    Re: Using the Patch function to either create OR update a record

    @Meneghino Awesome code! Simple and worked like a charm !

     

    How I could not figure this out earlier ...

     

    Anyway, all credits to your solution, added to dev. pattern inside company over here.

     

    Cheers and thanks a lot!

  • Community Power Platform Member Profile Picture
    on 31 Jul 2019 at 10:50:27
    Re: Using the Patch function to either create OR update a record

    Hi @Meneghino 

     

    I have a problem when pressing the Send button, the form should be reset. When you add a new item, it is added to the list as a new item and not merged with the last item saved in the list.
    How can I press the Send button? The form is reset as if I opened the application again.
    Note that I used ResetForm but unfortunately did not work.


    Thank you.

  • Community Power Platform Member Profile Picture
    on 15 Aug 2018 at 13:35:49
    Re: Using the Patch function to either create OR update a record

    Hi

     

    I am a newbie in Power Apps. I only wanted to save records into an Excel file when I press a button.

    I am quite confused of how to do it.

     

    What code should I use? and how the syntax arrangements will be?

    screenshot1.jpgscreenshot2.jpg

     

    I created my own form to begin with. I wanted to save the input data from that form to a local EXCEL file that I have

     

    Thanks,

  • gcanelon Profile Picture
    46 on 04 Aug 2018 at 15:42:39
    Re: Using the Patch function to either create OR update a record

    Hello J. Yes, it's the post marked as a solution here. That worked for me. I hope it does for you.

     

    Regards.

  • jcollins Profile Picture
    149 on 31 Jul 2018 at 00:34:41
    Re: Using the Patch function to either create OR update a record

    hi there, did you ever find a solution to this? I'm having the same issue... 

  • ngabougaifan Profile Picture
    54 on 27 Jul 2018 at 09:53:23
    Re: Using the Patch function to either create OR update a record

    Hi I have faced something similar and need some help !

     

    I have the submit button with the OnSelect function "Patch(Table1,Defaults(Table1),Form9.Updates,Form10.Updates)"

     

    However, when I wanted to edit and update the records, it will create a new row of record instead of updating the selected record. How could I work around with this ?

  • Verified answer
    gcanelon Profile Picture
    46 on 23 Aug 2017 at 17:27:05
    Re: Using the Patch function to either create OR update a record

    Awesome Meneghino, it works flawlessly. I tried it with several users and its smooth and does what it's intended to do. Thanks for your help. Its greatly appreciated.

     

    Regards.

  • Verified answer
    Meneghino Profile Picture
    6,949 on 23 Aug 2017 at 08:45:34
    Re: Using the Patch function to either create OR update a record

    Hi @gcanelon

    Yes, I think the issue is that MyPatchedRecord is a table and not a record.  Try this instead:

    ClearCollect(MyPatchedRecord, Patch('Ubicacion de personal de recate',{ID:CurrentUserID}, {Title: CurrentUser.FullName, Latitud:Location.Latitude,Longitud:Location.Longitude}));
    If(IsBlank(CurrentUserID), Set(CurrentUserID, Last(MyPatchedRecord).ID))

    Please let me know

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Apps - Building Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 89 Most Valuable Professional

#2
MS.Ragavendar Profile Picture

MS.Ragavendar 60

#3
stampcoin Profile Picture

stampcoin 48

Overall leaderboard