cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted

Updating variable of type Record

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Power Apps Staff Mr-Dang-MSFT
Power Apps Staff

Re: Updating variable of type Record

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:

  • For variables pointing to records from a data source, it's better to recapture the record by a LookUp or ThisItem (if you're in the context of a gallery). This maintains the schema of fields that the variable expects.
  • The Patch() in this method does not modify a variable, collection, or data source. It simply returns a merged record in this scenario. You need to set its result to the variable again to achieve the "update."
  • Consistent schema makes a happy app. A common user error with this method happens when mismatching the number of fields in the record throughout the app. Maintain the schema for best results.

 

Onward to the solution:

Doc: https://docs.microsoft.com/powerapps/maker/canvas-apps/functions/function-patch#overview

 

Big idea:

  • Patch lets you merge the contents of two or more records whose fields may differ (I explain about records later)
  • Fields of the same name and hierarchy in the record will be determined by the last record in the Patch. Last one wins.

 

 

 

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.

  • I keep varTheme as the first argument of Patch to maintain the schema. I want the field structure and the values of any untouched fields to remain the same.
  • So Colors will remain the same since it's already in varTheme and the second record doesn't overlap it.

 

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.

View solution in original post

10 REPLIES 10
Eelman
Level 10

Re: Updating variable of type Record

@FredericForest 

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 ...

 

 

WarrenBelz
Level 8

Re: Updating variable of type Record

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.

Super User
Super User

Re: Updating variable of type Record

@FredericForest 

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."

Re: Updating variable of type Record

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 !

Super User
Super User

Re: Updating variable of type Record

@FredericForest 

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?

Re: Updating variable of type Record

@mdevaney 
Sure, will do ! And my twitter handle is @fredericForest7.
Cheers !

Super User
Super User

Re: Updating variable of type Record

@FredericForest 

Just followed you.  @mattbdevaney for me.  I'm looking forward to your planned post.

Power Apps Staff Mr-Dang-MSFT
Power Apps Staff

Re: Updating variable of type Record

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:

  • For variables pointing to records from a data source, it's better to recapture the record by a LookUp or ThisItem (if you're in the context of a gallery). This maintains the schema of fields that the variable expects.
  • The Patch() in this method does not modify a variable, collection, or data source. It simply returns a merged record in this scenario. You need to set its result to the variable again to achieve the "update."
  • Consistent schema makes a happy app. A common user error with this method happens when mismatching the number of fields in the record throughout the app. Maintain the schema for best results.

 

Onward to the solution:

Doc: https://docs.microsoft.com/powerapps/maker/canvas-apps/functions/function-patch#overview

 

Big idea:

  • Patch lets you merge the contents of two or more records whose fields may differ (I explain about records later)
  • Fields of the same name and hierarchy in the record will be determined by the last record in the Patch. Last one wins.

 

 

 

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.

  • I keep varTheme as the first argument of Patch to maintain the schema. I want the field structure and the values of any untouched fields to remain the same.
  • So Colors will remain the same since it's already in varTheme and the second record doesn't overlap it.

 

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.

View solution in original post

Re: Updating variable of type Record

Thanks for the very complete answer @Mr-Dang-MSFT 😉 

Cheers !

Helpful resources

Announcements
thirdimage

Power Apps Super User Class of 2020

Check it out!

thirdimage

New Badges

Check it out!

thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

SecondImage

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Solution Authors
Top Kudoed Authors
Users online (5,363)