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}
)
);
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 .
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
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.
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.
WarrenBelz
85
Most Valuable Professional
Michael E. Gernaey
65
Super User 2025 Season 1
mmbr1606
55
Super User 2025 Season 1