cancel
Showing results for 
Search instead for 
Did you mean: 

Update single attribute of a record variable

It is currently possible, and quite useful, to create global of screen variables containing a record (or what us, old-timers, used to call a data structure):

 

UpdateContext({MyRecord:{Id:1, Description: “Some text”, “IsCompleted”:false}});

We can then refer to a specific attribute of this variable in functions:

 

Label.Color = If(MyRecord.IsCompleted, Black, Red)

However, it is not possible to update only one attribute of the variable without recreating the complete record, which is a pain when the record contains many attributes.  Possible solutions could be :

 

UpdateContext({MyRecord.IsCompleted : true})
Patch(,MyRecord,{IsCompleted : true}) //note that the first parameter of patch is omitted
UdpateRecord(MyRecord,{IsCompleted : true})  //New function
Status: Completed
Comments
CarlosFigueira
Power Apps

This is actually supported today, with a little known variation of the Patch function. From its documentation:

 

Merge records outside of a data source

Specify two or more records that you want to merge. Records are processed in order from the beginning of the argument list to the end, with later property values overriding earlier ones.

Patch returns the merged record and doesn't modify its arguments or records in any data sources.

 

In your scenario, you can have the following expression to update the IsCompleted property of your record:

UpdateContext({ MyRecord: Patch(MyRecord, { IsCompleted: true }) })

Hope this helps!

CarlosFigueira
Power Apps
Status changed to: Completed
 
mhebert
Advocate II

Oh, 

 

Thanks for the info.  

 

 

 

 

mhebert
Advocate II

Hi

 

the proposed solution does not work. When trying this synthax, I get an error satyng tha the first parameters of Patch must be a collection....

Capture.PNG

 

CarlosFigueira
Power Apps

The error message is incorrect - Patch can be used to update a record. You can check the app at https://1drv.ms/u/s!AonXZuy7S5iZiapY2mXJmNoBihGjSg for an example of Patch being used to update the record.

 

What are the other places where SelectedInspection is defined? Is there an error in one of them?

mhebert
Advocate II

Carlos,

 

SelectionInspection is defined as part of the result of a REST call.

 

Thanks for the reply.  The line I showned in error was an attempt to initialize the SelectionInspection to a new, blank instance of the record (similar to NEW oeprator in OO). It may have cause this issue.

 

I tried the Patch function on an initialised instances and it does work. 

 

Case solve

 

Thanks

mhebert
Advocate II

One more observation,

 

The synthax worked when it is added in a function after the record variable has been initialized. However, whenever I close the application and open it again, all functions that refer to this record variable are in error stating the variable usage is not compatible. They all remain in error until I comment out the lines where I patch the record. 

 

I will need to find workaround to implement the logic I need as this is obviously not stable.

 

 

Romansx
Advocate IV

same problem @mhebert... when you gonna fix this? @CarlosFigueira any info or data to whats happening?? thanks

CarlosFigueira
Power Apps

@mhebert@Romansx I haven't been able to reproduce this - I have a couple of apps where I do use this syntax to update an object, and I have been able to update them without problems.

 

Is it possible for one of you to reproduce this issue, and either send me an app that shows this problem, or instructions to build such an app? Thanks!

Romansx
Advocate IV

I managed to initialize the variable in "onvisible" screen and worked for me. Now i encounter other problem and is that i tried to perform the same logic but with the "Set" function and the "error on the data types" appears same when tried "UpdateContext"... What happens here?? Please @CarlosFigueira... Thanks...