Hello,
I wonder if there is a way to update a variable of type Record.
Imagine the following code :
Set(varTheme, {
Colors : {
Color1 : RGBA(255,0,0,1)
,Color2 : RGBA(0,255,0,1)
}
,Size : {
Height : 40
,Width : 200
}
}
)
I can easily use this variable in my app by calling it in a property
Height = varTheme.Size.Height
Now the question is : do you know a way to update part of that variable without overwriting the whole thing ? Something like...
Set(varTheme.Size.Height, 50)
I tried a few things with Update and Patch to no avail since these function expect a table as a source.
Arguably, there are many workaround to this, but I wondered if anyone has a way to make this work.
Solved! Go to Solution.
Hi @FredericForest,
It is possible to merge two or more records together with Patch and set its result back to the variable to achieve the effects of an update. While it has the intended effect, I would not call it a feature to update a field of a complex variable.
Some notes:
Onward to the solution:
Doc: https://docs.microsoft.com/powerapps/maker/canvas-apps/functions/function-patch#overview
Big idea:
Set(varTheme,
Patch(
varTheme,
{
Size: {
Height: 40*2,
Width: 200*2
}
}
)
)
In the example above, this means "Merge the existing variable varTheme and a manually entered record enclosed by {}. Set its result back to the variable varTheme." My goal is to merge some changes to the Size fields: multiply the integers by 2.
How do you modify only one of the fields though?
Would this work: modify only the height and not the width?
Set(varTheme,
Patch(
varTheme,
{
Size: {
Height: 40*2
}
}
)
)
{Size: {height: #, width: #}} is a record with Size containing a nested record of two fields
{Size: {height: #}} is a record with Size containing a nested record of only one field
Since the "last one wins" in the merger, what happens to Size? It is not just a string or value; it has nested fields that needs to be resolved for the merger as well. Since the second record is smaller in the example above, Size does not merge because Width is missing. If the last record has all the fields of the earlier records and more, it would merge.
Let me know if you need clarification on any part.
I possibly tried everything you have, but alas, no success either.
Because I was unsure of your usage I built a workaround where I added the Set(varTheme ...) to the OnSelect of a button, with this change:
Height : Value(txtHeight.Text)
Then added a textinput field named txtHeight and set it's OnChange to Select(Button1).
Doing this for Colors gets a little messy though because all of RGBA are values, meaning a TextInput for each ...
Hi @FredericForest ,
I am not sure if this is the track that suits you, but I manage all of my control attributes from "master" items (TextBox / DropDown / DatePicker/ Labels) on an "Admin" screen.
This includes all the attributes you mention plus things like Fill and Font Name/Weight/Size/Color. Some of these are set to variable options, so the whole look and design of the app or screen can be changed with a few variables.
It works fine with nothing I have detected in speed penalties.
Maybe this is an alternate method of doing what you need.
I am happy to elaborate further on this if you need.
Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.
I like your idea to store the Theme as a record in a SET variable and perform changes using PATCH.
Its definitely possible but I recommend you create a single level record variable instead of adding multiple levels with tables. This will reduce the complexity of your PATCH statement.
My suggested layout looks like this.
Set(varTheme, {
Color_Color1 : RGBA(255,0,0,1),
Color_Color2 : RGBA(0,255,0,1),
Size_Height : 40,
Size_Width : 200
}
)
Any value requiring a change can be patched like this.
Set(varTheme, Patch(varTheme, {Size_Height: 105}))
The theme variable would be used like this.
varTheme.Size_Height
---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."
Hi guys,
Thanks for the input. To be clear, this is just for the sake of knowing if it can be done rather than solving a real problem. It's part of a demo for an internal training I'm putting together at the moment. I'll check on the twittosphere if anyone's got an idea and, if not, mark it as solved.
On a side note, I do have a fairly comprehensive varTheme variable that I initialize on app start. Since it includes multiple grid systems, having multiple levels make sense there; and since it's purely local arithmetics, it really doesn't impact loading times. I'll try to post the code and explain how it works in a blog post when I get a chance. It's a good time-saver when I start a new project !
I would love to see the varTheme variable used in your app OnStart and compare it to my own. This is where the real learning occurs. Make sure to send me a DM here or link via Twitter once the blog post is done. Do you have a Twitter handle I can follow you on?
Just followed you. @mattbdevaney for me. I'm looking forward to your planned post.
Hi @FredericForest,
It is possible to merge two or more records together with Patch and set its result back to the variable to achieve the effects of an update. While it has the intended effect, I would not call it a feature to update a field of a complex variable.
Some notes:
Onward to the solution:
Doc: https://docs.microsoft.com/powerapps/maker/canvas-apps/functions/function-patch#overview
Big idea:
Set(varTheme,
Patch(
varTheme,
{
Size: {
Height: 40*2,
Width: 200*2
}
}
)
)
In the example above, this means "Merge the existing variable varTheme and a manually entered record enclosed by {}. Set its result back to the variable varTheme." My goal is to merge some changes to the Size fields: multiply the integers by 2.
How do you modify only one of the fields though?
Would this work: modify only the height and not the width?
Set(varTheme,
Patch(
varTheme,
{
Size: {
Height: 40*2
}
}
)
)
{Size: {height: #, width: #}} is a record with Size containing a nested record of two fields
{Size: {height: #}} is a record with Size containing a nested record of only one field
Since the "last one wins" in the merger, what happens to Size? It is not just a string or value; it has nested fields that needs to be resolved for the merger as well. Since the second record is smaller in the example above, Size does not merge because Width is missing. If the last record has all the fields of the earlier records and more, it would merge.
Let me know if you need clarification on any part.
User | Count |
---|---|
168 | |
96 | |
79 | |
72 | |
59 |
User | Count |
---|---|
210 | |
167 | |
98 | |
93 | |
78 |