web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Any Patch Experts Here?
Power Apps
Answered

Any Patch Experts Here?

(0) ShareShare
ReportReport
Posted on by 514


Hi

I am trying to do a patch insert or update, depending on values held.

I am using a variable called FindID to do a lookup to see if the record exists in the Sharepoint list.

If(VarMode="Edit",Set(FindID,First(MyCollect.ID)),Set(FindID, LastAddedItem.ID);

Set(myItem,LookUp(ListName,ID = FindID);

 

However,
=FindID is saying name isn't valid. This identifer isn't recognised.

 

Any help appreciated.

 

Thanks

Categories:
  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @sienna28 

    There are a couple ways to go about what you are trying to do, but with the method you have, there are some errors in your formula.  Consider the following instead:

    If(VarMode="Edit",
    Set(FindID, 
    If(VarMode="Edit", First(MyCollect).ID, LastAddedItem.ID
    ) ); Set(myItem,LookUp(ListName,ID = FindID);

    Which can be simplified to the following:

    Set(myItem,
     LookUp(ListName, 
     ID = If(varMode="Edit", 
     First(MyCollect).ID,
     LastAddedItem.ID
     )
     )
     )

    I hope this is helpful for you.

     

  • sienna28 Profile Picture
    514 on at

    Thanks for your reply.

     

    I added the code, but it seemed to throw up errors.

    pa1.png
    pa2.png
  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @sienna28 

    Your second formula is the one that is more direct to the source, so I will look at that one.

    I am not sure why you would put this in an If statement to look at the "Edit" value in two places...that is not making sense.

    Set your formula to this and see if that produces any error:

    Set(myItem,
     LookUp(ListName, 
     ID = If(varMode="Edit", 
     First(MyCollect).ID,
     LastAddedItem.ID
     )
     )
     )
  • sienna28 Profile Picture
    514 on at

    It may help if I explain what I am attempting.

     

    On the 1st screen I am trying to do an insert and update (it could be that they want to update the record they just inserted).

     

    On another screen which has a list of records, I click one from a datatable - I was trying to pass the ID of the record to the 1st screen so that it could be edited.

     

    I am finding that a second record gets inserted rather than updating the one on the screen.

     

    That is why I had an edit variable flag. It appears I may be going about it the wrong way.

    Any pointers appreciated.

     

    Thank you

     

     

     

     

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @sienna28 

    I think you might be overcomplicating the process.  Especially when you start creating variables to track and hold and so forth.  You have to remember that variables are your responsibility.  So, if you want something to update based on a variable...you have to constantly keep it updated.

     

    So, based on your plan...

    Are you are using an EditForm on the 1st screen, or is this a form of your own setup?  If not, then we can adjust for that too.

     

  • sienna28 Profile Picture
    514 on at

    I'm not using forms in this app.

    I created a blank screen and added controls so that I could use the patch command.

     

    I have a main screen which allows insert and update of records to a Sharepoint list.

     

    Another screen contains a data table with a list of records entered. Clicking on one would take me back to the main screen for editing and saving. It loads the record onto the main screen fine, but as you say I may be going wrong by maintaining an edit variables.

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @sienna28 

    That is all good.  Very easy to implement your own form with control, you just do need to do a little more overhead on it.

     

    As I think through your scenario, I wonder how one distinguishes between a new and update.  In other words, if a user clicks on an item in the datatable, then they are taken to the "edit form" (referring to your custom form)/Screen 1

    When they are done, I assume you have a "save" action.  Is there a "new" action someplace?  How does one create a new record?  Or, does one create only one record when they first come to the "edit form" and then from there they can click on items in the datatable and edit/update them?

     

     

  • sienna28 Profile Picture
    514 on at

    I think I cracked it.

    Your bracketing formula helped a lot.

    When a record is clicked on the other screen in the data table, I set a variable for the ID called DataTableID.

    In the main screen I then check to see if this has a value - if it does then I do the lookup with that value. If not then I use the last inserted ID. The record is then updated and patched using either of those ID's. The reason it was creating a second record was because I was not resetting it to blank from the main menu if the new record option is selected. There are probably a million ways to write this more elegantly than I have, but I am a beginner who is learning. Thanks for your help and pointing me in the right direction.

     


    Set(myItem,
    LookUp(MyData,
    ID = If(!IsBlank(DataTableID),
    DataTableID,
    LastAddedItem.ID
    )
    )
    )
    ;

    If(!IsBlank(myItem),
    UpdateContext({msg:"Updating"});
    UpdateContext({patchResult:
    Patch(MyData,
    {ID: If(!IsBlank(DataTableID),
    DataTableID,
    LastAddedItem.ID)},
    {
    Title:txtClientName.Text,
    WriteOffTotal:Value(WriteOffAmt.Text),
    AccountReference :txtAcctRef.Text
    }

    )
    });

    UpdateContext({msg:If(Not(IsBlank(patchResult)) And IsEmpty(Errors(MyData)),"Update Patch succeeded","Update Patch failed")});

    Refresh(MyData);

    ,


    UpdateContext({msg:"Adding Rec"});
    UpdateContext({patchResult:
    Patch(
    MyData,
    Defaults( MyData ),

    {
    Title:txtClientName.Text,
    WriteOffTotal:Value(WriteOffAmt.Text),
    AccountReference :txtAcctRef.Text
    }

    )

    });

    UpdateContext({msg:If(Not(IsBlank(patchResult)) And IsEmpty(Errors(MyData)),"Insert Patch succeeded","Insert Patch failed")});

    Refresh(MyData);
    Set(LastAddedItem,First(Sort(MyData,ID,SortOrder.Descending)))

    )

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @sienna28 

    Sounds good! 

    So here would be a little different way to put that formula together with the same results:

    Set(myItem, LookUp(MyData, ID = Coalesce(DataTableID, LastAddedItem.ID)) );
    UpdateContext({msg:If(!IsBlank(myItem), "Updating", "Adding" Rec}),
    UpdateContext(
     {patchResult:
     Patch(MyData,
     Coalesce(myItem, Defaults(MyData)),
     {
    		Title:txtClientName.Text,
    		WriteOffTotal:Value(WriteOffAmt.Text),
    		AccountReference :txtAcctRef.Text
     }
     )
     });
    UpdateContext({msg: "Update Patch " & If(!(IsBlank(patchResult)) && IsEmpty(Errors(MyData)), "succeeded", "failed")});
    If(IsBlank(MyItem), Set(LastAddedItem, patchResult))

    This combines several areas in your formula that were repeated based on If conditions into a bit more manageable and maintainable ways.

    You really don't need the Refresh of the datasource since you are patching it directly.

    For the LastAddedItem, you really only need to grab that when you actually "add" an item.  

    Theoretically, the above formula is the same results that you had with the longer...just for example as you learn more about PowerApps as you mention.

    Also, it was written freehand, so there may be some syntax errors, but the functionality is there.

     

  • sienna28 Profile Picture
    514 on at

    I tried that new code and it works well.
    Much cleaner than my effort.


    Never knew about Coalesce, but after Googling it, it appears to return the item which has data.

     

    The only thing I am confused on is the last statement.

    If(IsBlank(MyItem), Set(LastAddedItem, patchResult))

     

    When patchResult is first called, does it hold a returned record value?

    The other thing I noticed is that when I put msg into a label, the label did not show any text.

     

    Thanks

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 224 Super User 2026 Season 2

#2
WarrenBelz Profile Picture

WarrenBelz 189 Most Valuable Professional

#3
Mohsin Ali Profile Picture

Mohsin Ali 126

Last 30 days Overall leaderboard