Power Apps Exchange

Please login or click SIGN UP FOR FREE to create your PowerAppsUG account to join this user group.
Expand all | Collapse all

Finding record to Patch

Jump to Best Answer
  • 1.  Finding record to Patch

    Top Contributor
    Posted Jun 02, 2020 09:42 AM
    I seem to be misunderstanding something basic about record IDs.
    I'm developing an app to track data about a fleet of vehicles, including driver name. I need to make sure that no one is assigned to more than one vehicle at a time. I have a SP list of drivers with various other data. I have a list of vehicles with various other data. The client would like to be able to view either list and see the driver name in the vehicle list and the vehicle number in the driver list.
    I have a form to change drivers. I create a collection on start that holds all drivers and another that holds drivers that are not assigned to vehicles. Both of these collections have the record ID from the driver list for each driver. I have a dropdown fed from the latter to allow for selecting the new driver. After the form is submitted to change the driver in the vehicle list, I need to Patch the correct record to change the info in the driver list. Here lies my problem.
    The Patch command is showing an error - it says "Invalid argument type (Number). Expecting a Record value instead." I was under the impression that a "Record value" was a number. I'm getting the ID from the SP list and using it as the record number. What should I be doing instead?
    Here is the code in question:
    Patch(Empl_Master_List, drp_DriverName.Selected.ID, {Vehicle:frm_DriverChange.LastSubmit.TruckNumber});


    ------------------------------
    Brian Scott
    Chief Creative Officer
    Concord NC
    ------------------------------


  • 2.  RE: Finding record to Patch

    Top Contributor
    Posted Jun 02, 2020 09:03 PM
    Hi Brian,
    I am assuming that the Record it is referring to is Vehicle and this is probably a Choice or a Lookup column?
    I also tend to use UpdateIf because of some still unresolved Patch issues.
    So if a Choice column
    UpdateIf(
       Empl_Master_List, 
       ID=drp_DriverName.Selected.ID, 
       {  
          Vehicle:
          {
             Value:frm_DriverChange.LastSubmit.TruckNumber
          }
       }
    );​

    if you have a lookup column, this gets a bit trickier



    ------------------------------
    Warren Belz
    Queensland Australia
    ------------------------------



  • 3.  RE: Finding record to Patch

    Top Contributor
    Posted Jun 03, 2020 07:01 AM
    No - I try to never use Choice or Lookup because of the issues - I just built that into the app. What the app appears to be complaining about is the
    drp_DriverName.Selected.ID for the record number. But that is from the collection and is the record ID of the record I want to Patch.
    I can try the UpdateIf.I can't use Last Submit, because the Patch is to a different list than the form was writing to.

    ------------------------------
    Brian Scott
    Chief Creative Officer
    Concord NC
    ------------------------------



  • 4.  RE: Finding record to Patch

    Top Contributor
    Posted Jun 03, 2020 08:11 AM
    I guess I'm not sure about the data types. I thought the record ID was a number, but the app seems to be expecting a "Record" not a number. Can you convert a number into the type of data Patch wants? I'm not seeing anything in the documentation, but I could be looking in the wrong places.

    ------------------------------
    Brian Scott
    Chief Creative Officer
    Concord NC
    ------------------------------



  • 5.  RE: Finding record to Patch
    Best Answer

    Top Contributor
    Posted Jun 03, 2020 10:11 AM
    I found a solution pretty much by accident!
    Here is the syntax that works:

    Patch(Empl_Master_List, {ID:drp_DriverName.Selected.ID}, {Vehicle:frm_DriverChange.LastSubmit.TruckNumber})

    Why the record needs to be in brackets I do not know - I did not see that listed in Microsoft's documentation. I saw a blog posting on a different 
    topic that showed that syntax, so I decided to give it a go, and it works!

    ------------------------------
    Brian Scott
    Chief Creative Officer
    Concord NC
    ------------------------------



  • 6.  RE: Finding record to Patch

    Top Contributor
    Posted Jun 03, 2020 06:24 PM
    Edited by Warren Belz Jun 03, 2020 06:29 PM
    Good to see Brian,
    Back in my time zone now.
    A few things (and sorry I did not correct your Patch code on the first bit). Patch does need {xxx:xxx} syntax to identify the target record (whereas UpdateIf is xxx=xxx).
    The second bit you were (obviously) correct on - I thought the issue was a Choice field at the target. I tend to use UpdateIf as a Patch bug sometimes will not write to currently blank fields in existing records.
    The last thing is that LastSubmit applies only to forms and is the value of the last record (in the current session) submitted by SubmitForm() on that form and is available after OnSuccess of the form submit, so if you did use SubmitForm for the record, it is available for either Patch or UpdateIf.
    If you Patched the new record, you can set a Variable to get the ID
    Set(
       vNewID,
       Patch(
          YourSPList,
          Defaults(YourSPList),
          {
             Field1:YourValue1,
             Field2:YourValue2,
             Field3:YourValue3
          }
       ).ID
    )​


    ------------------------------
    Warren Belz
    Queensland Australia
    ------------------------------



  • 7.  RE: Finding record to Patch

    Top Contributor
    Posted Jun 04, 2020 08:27 AM
    I guess what I'm still a little confused about is the use of the variable. When I set a variable to a record ID, why can't I use that variable directly as the record ID, why do I need to use the { } syntax?

    ------------------------------
    Brian Scott
    Chief Creative Officer
    Concord NC
    ------------------------------