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 / Set(selectedRecord, Pa...
Power Apps
Answered

Set(selectedRecord, Patch(table, selectedRecord, {column: value})

(0) ShareShare
ReportReport
Posted on by 61

I have a global variable named selectedRecord representing a record from the table 'table'.

 

Can I use the following code to update the record and global variable. Note that I am using selectedRecord in the manner of var=function(var). Is there a better way to do this?

 

Set(
 selectedRecord,
 Patch(
 table,
 If(IsBlank(selectedRecord),
 Defaults(table),
 selectedRecord
 ),
 {column: value}
 )
);

 

 

I have the same question (0)
  • cchannon Profile Picture
    4,702 Moderator on at

    That is totally unnecessary. All you need to do is Patch. Power Apps will update the definition of the table in your app when you do a Patch, so selected Record will also be updated.

  • RaymondWood Profile Picture
    61 on at

    Thanks for the response @cchannon .

     

    Context:

    selectedRecord is a global variable of type {record}, not a record.id.  It is initially set in the app by clicking on a gallery item representing the Table. The values in the record are displayed on every screen so instead of doing another lookup on each screen refresh I use the properties of the selectedRecord variable. So yes I want to keep the values in the variable up to date with the database thereby reducing the need for more calls.

     

    My questions was, is it invalid code or should it work as written? Something in my code isn't working and I'm not sure what it is.

  • Verified answer
    cchannon Profile Picture
    4,702 Moderator on at

    OK, I still feel it is not necessary and that you could streamline your app overall with Formulas here, but in that case, if selectedRecord is a locally stored Record and not a reference to the record in table, then I am pretty sure you cannot use it to Patch. You have to use it to LookUp the record to patch.

     

    Patch(
     MyTable,
     LookUp(MyTable, MyRecordId = selectedRecord.MyRecordId),
     { column: value }
    )

     

    Then as long as that part works, yes, the Patch operation returns a Record, so it is valid to use it in your Set(selectedRecord... operation

  • Verified answer
    RaymondWood Profile Picture
    61 on at

    Thanks @cchannon

     

    I had time to validate the code.  The following method seems to be working though I cannot explain how or why. Data makes it to the table each time. I can even reference the gblVar as the record (instead of doing Lookup(Table, GUID = gblVar.GUID) in the second line of the patch). So its really quite eloquent. 

     

     

    // Initial choice of record to work with
    Set(
     gblVar, 
     Lookup(
     Table, 
     GUID-Column = GUID("al89-7609...")
     )
    );
    
    // Patching to record several times throughout the app
    Set(
     gblVar,
     Patch(
     Table,
     gblVar,
     {Column: Value}
     )
    );

     

     

     

    In any case I found my problem was actually in trying to do this:

     

     

    Set(
     gblVar,
     Patch(
     Table,
     gblVar,
     {Column: NamedFormula}
     )
    );

     

     

     Named Formulas do not work with Patch!

     

    Thanks again for your assistance. I feel like using the return from a necessary Patch instead of several Lookups to access the information in the table is more efficient. This is especially true when I have to make several patches to the same record throughout the app. A Named Formula would simply perform another call to the database and they cannot be used with Patch. 

     

    Then again this method would be problematic if the Patch returned an error (lightbulb). Shoot. Back to the drawing board.

     

    Cheers @cchannon .

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 Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Vish WR Profile Picture

Vish WR 414

#2
Valantis Profile Picture

Valantis 408

#3
timl Profile Picture

timl 339 Super User 2026 Season 1

Last 30 days Overall leaderboard