cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
FredericForest
Kudo Kingpin
Kudo Kingpin

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

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

12 REPLIES 12
Anonymous
Not applicable

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

 

 

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.

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

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 !

@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?

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

@FredericForest 

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:

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

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

Cheers !

Helpful resources

Announcements

Power Platform Connections - Episode 7 | March 30, 2023

Episode Seven of Power Platform Connections sees David Warner and Hugo Bernier talk to Dian Taylor, alongside the latest news, product reviews, and community blogs.     Use the hashtag #PowerPlatformConnects on social media for a chance to have your work featured on the show.     

Announcing | Super Users - 2023 Season 1

Super Users – 2023 Season 1    We are excited to kick off the Power Users Super User Program for 2023 - Season 1.  The Power Platform Super Users have done an amazing job in keeping the Power Platform communities helpful, accurate and responsive. We would like to send these amazing folks a big THANK YOU for their efforts.      Super User Season 1 | Contributions July 1, 2022 – December 31, 2022  Super User Season 2 | Contributions January 1, 2023 – June 30, 2023    Curious what a Super User is? Super Users are especially active community members who are eager to help others with their community questions. There are 2 Super User seasons in a year, and we monitor the community for new potential Super Users at the end of each season. Super Users are recognized in the community with both a rank name and icon next to their username, and a seasonal badge on their profile.    Power Apps  Power Automate  Power Virtual Agents  Power Pages  Pstork1*  Pstork1*  Pstork1*  OliverRodrigues  BCBuizer  Expiscornovus*  Expiscornovus*  ragavanrajan  AhmedSalih  grantjenkins  renatoromao    Mira_Ghaly*  Mira_Ghaly*      Sundeep_Malik*  Sundeep_Malik*      SudeepGhatakNZ*  SudeepGhatakNZ*      StretchFredrik*  StretchFredrik*      365-Assist*  365-Assist*      cha_cha  ekarim2020      timl  Hardesh15      iAm_ManCat  annajhaveri      SebS  Rhiassuring      LaurensM  abm      TheRobRush  Ankesh_49      WiZey  lbendlin      Nogueira1306  Kaif_Siddique      victorcp  RobElliott      dpoggemann  srduval      SBax  CFernandes      Roverandom  schwibach      Akser  CraigStewart      PowerRanger  MichaelAnnis      subsguts  David_MA      EricRegnier  edgonzales      zmansuri  GeorgiosG      ChrisPiasecki  ryule      AmDev  fchopo      phipps0218  tom_riha      theapurva  takolota     Akash17  momlo     BCLS776  Shuvam-rpa     rampprakash  ScottShearer     Rusk  ChristianAbata     cchannon  Koen5     a33ik   Heartholme     AaronKnox        Matren        Alex_10        Jeff_Thorpe        poweractivate        Ramole        DianaBirkelbach        DavidZoon        AJ_Z        PriyankaGeethik        BrianS        StalinPonnusamy        HamidBee        CNT        Anonymous_Hippo        Anchov        KeithAtherton        alaabitar        Tolu_Victor        KRider        sperry1625        IPC_ahaas      zuurg    rubin_boer   cwebb365   Dorrinda   G1124   Gabibalaban   Manan-Malhotra   jcfDaniel   WarrenBelz   Waegemma      If an * is at the end of a user's name this means they are a Multi Super User, in more than one community. Please note this is not the final list, as we are pending a few acceptances.  Once they are received the list will be updated. 

Microsoft Power Platform Conference | Registration Open | Oct. 3-5 2023

We are so excited to see you for the Microsoft Power Platform Conference in Las Vegas October 3-5 2023! But first, let's take a look back at some fun moments and the best community in tech from MPPC 2022 in Orlando, Florida.   Featuring guest speakers such as Charles Lamanna, Heather Cook, Julie Strauss, Nirav Shah, Ryan Cunningham, Sangya Singh, Stephen Siciliano, Hugo Bernier and many more.   Register today: https://www.powerplatformconf.com/   

Check out the new Power Platform Communities Front Door Experience!

We are excited to share the ‘Power Platform Communities Front Door’ experience with you!   Front Door brings together content from all the Power Platform communities into a single place for our community members, customers and low-code, no-code enthusiasts to learn, share and engage with peers, advocates, community program managers and our product team members. There are a host of features and new capabilities now available on Power Platform Communities Front Door to make content more discoverable for all power product community users which includes ForumsUser GroupsEventsCommunity highlightsCommunity by numbersLinks to all communities Users can see top discussions from across all the Power Platform communities and easily navigate to the latest or trending posts for further interaction. Additionally, they can filter to individual products as well.       Users can filter and browse the user group events from all power platform products with feature parity to existing community user group experience and added filtering capabilities.     Users can now explore user groups on the Power Platform Front Door landing page with capability to view all products in Power Platform.    Explore Power Platform Communities Front Door today. Visit Power Platform Community Front door to easily navigate to the different product communities, view a roll up of user groups, events and forums.

Welcome to the Power Apps Community

Welcome! Congratulations on joining the Microsoft Power Apps community! You are now a part of a vibrant group of peers and industry experts who are here to network, share knowledge, and even have a little fun! Now that you are a member, you can enjoy the following resources:   The Microsoft Power Apps Community Forums If you are looking for support with any part of Microsoft Power Apps, our forums are the place to go. They are titled "Get Help with Microsoft Power Apps " and there you will find thousands of technical professionals with years of experience who are ready and eager to answer your questions. You now have the ability to post, reply and give "kudos" on the Power Apps community forums! Make sure you conduct a quick search before creating a new post because your question may have already been asked and answered!   Microsoft Power Apps IdeasDo you have an idea to improve the Microsoft Power Apps experience, or a feature request for future product updates? Then the "Power Apps Ideas" section is where you can contribute your suggestions and vote for ideas posted by other community members. We constantly look to the most voted Ideas when planning updates, so your suggestions and votes will always make a difference.   Community Blog & NewsOver the years, more than 600 Power Apps Community Blog Articles have been written and published by our thriving community. Our community members have learned some excellent tips and have keen insights on building Power Apps. On the Power Apps Community Blog, read the latest Power Apps related posts from our community blog authors around the world. Let us know if you would like to become an author and contribute your own writing — everything Power Apps related is welcome!   Power Apps Samples, Learning and Videos GalleriesOur galleries have a little bit of everything to do with Power Apps. Our galleries are great for finding inspiration for your next app or component. You can view, comment and kudo the apps and component gallery to see what others have created! Or share Power Apps that you have created with other Power Apps enthusiasts. Along with all of that awesome content, there is the Power Apps Community Video & MBAS gallery where you can watch tutorials and demos by Microsoft staff, partners, and community gurus in our community video gallery.   Again, we are excited to welcome you to the Microsoft Power Apps community family! Whether you are brand new to the world of process automation or you are a seasoned Power Apps veteran. Our goal is to shape the community to be your ‘go to’ for support, networking, education, inspiration and encouragement as we enjoy this adventure together!   Let us know in the Community Feedback if you have any questions or comments about your community experience.To learn more about the community and your account be sure to visit our Community Support Area boards to learn more! We look forward to seeing you in the Power Apps Community!The Power Apps Team

Top Solution Authors
Top Kudoed Authors
Users online (3,432)