Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Power Apps Pro Dev & ISV
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}
 )
);

 

 

  • Verified answer
    RaymondWood Profile Picture
    61 on at
    Re: Set(selectedRecord, Patch(table, selectedRecord, {column: value})

    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 .

  • Verified answer
    cchannon Profile Picture
    4,702 Super User 2025 Season 1 on at
    Re: Set(selectedRecord, Patch(table, selectedRecord, {column: value})

    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

  • RaymondWood Profile Picture
    61 on at
    Re: Set(selectedRecord, Patch(table, selectedRecord, {column: value})

    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.

  • cchannon Profile Picture
    4,702 Super User 2025 Season 1 on at
    Re: Set(selectedRecord, Patch(table, selectedRecord, {column: value})

    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.

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

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 - Power Apps Pro Dev & ISV

#1
WarrenBelz Profile Picture

WarrenBelz 85 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 65 Super User 2025 Season 1

#3
mmbr1606 Profile Picture

mmbr1606 55 Super User 2025 Season 1

Overall leaderboard