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

 

 

WarrenBelz
Most Valuable Professional
Most Valuable Professional

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

Celebrating the May Super User of the Month: Laurens Martens

  @LaurensM  is an exceptional contributor to the Power Platform Community. Super Users like Laurens inspire others through their example, encouragement, and active participation. We are excited to celebrated Laurens as our Super User of the Month for May 2024.   Consistent Engagement:  He consistently engages with the community by answering forum questions, sharing insights, and providing solutions. Laurens dedication helps other users find answers and overcome challenges.   Community Expertise: As a Super User, Laurens plays a crucial role in maintaining a knowledge sharing environment. Always ensuring a positive experience for everyone.   Leadership: He shares valuable insights on community growth, engagement, and future trends. Their contributions help shape the Power Platform Community.   Congratulations, Laurens Martens, for your outstanding work! Keep inspiring others and making a difference in the community!   Keep up the fantastic work!        

Check out the Copilot Studio Cookbook today!

We are excited to announce our new Copilot Cookbook Gallery in the Copilot Studio Community. We can't wait for you to share your expertise and your experience!    Join us for an amazing opportunity where you'll be one of the first to contribute to the Copilot Cookbook—your ultimate guide to mastering Microsoft Copilot. Whether you're seeking inspiration or grappling with a challenge while crafting apps, you probably already know that Copilot Cookbook is your reliable assistant, offering a wealth of tips and tricks at your fingertips--and we want you to add your expertise. What can you "cook" up?   Click this link to get started: https://aka.ms/CS_Copilot_Cookbook_Gallery   Don't miss out on this exclusive opportunity to be one of the first in the Community to share your app creation journey with Copilot. We'll be announcing a Cookbook Challenge very soon and want to make sure you one of the first "cooks" in the kitchen.   Don't miss your moment--start submitting in the Copilot Cookbook Gallery today!     Thank you,  Engagement Team

Announcing Power Apps Copilot Cookbook Gallery

We are excited to share that the all-new Copilot Cookbook Gallery for Power Apps is now available in the Power Apps Community, full of tips and tricks on how to best use Microsoft Copilot as you develop and create in Power Apps. The new Copilot Cookbook is your go-to resource when you need inspiration--or when you're stuck--and aren't sure how to best partner with Copilot while creating apps.   Whether you're looking for the best prompts or just want to know about responsible AI use, visit Copilot Cookbook for regular updates you can rely on--while also serving up some of your greatest tips and tricks for the Community. Check Out the new Copilot Cookbook for Power Apps today: Copilot Cookbook - Power Platform Community.  We can't wait to see what you "cook" up!      

Tuesday Tip | How to Report Spam in Our Community

It's time for another TUESDAY TIPS, your weekly connection with the most insightful tips and tricks that empower both newcomers and veterans in the Power Platform Community! Every Tuesday, we bring you a curated selection of the finest advice, distilled from the resources and tools in the Community. Whether you’re a seasoned member or just getting started, Tuesday Tips are the perfect compass guiding you across the dynamic landscape of the Power Platform Community.   As our community family expands each week, we revisit our essential tools, tips, and tricks to ensure you’re well-versed in the community’s pulse. Keep an eye on the News & Announcements for your weekly Tuesday Tips—you never know what you may learn!   Today's Tip: How to Report Spam in Our Community We strive to maintain a professional and helpful community, and part of that effort involves keeping our platform free of spam. If you encounter a post that you believe is spam, please follow these steps to report it: Locate the Post: Find the post in question within the community.Kebab Menu: Click on the "Kebab" menu | 3 Dots, on the top right of the post.Report Inappropriate Content: Select "Report Inappropriate Content" from the menu.Submit Report: Fill out any necessary details on the form and submit your report.   Our community team will review the report and take appropriate action to ensure our community remains a valuable resource for everyone.   Thank you for helping us keep the community clean and useful!

Community Roundup: A Look Back at Our Last 10 Tuesday Tips

As we continue to grow and learn together, it's important to reflect on the valuable insights we've shared. For today's #TuesdayTip, we're excited to take a moment to look back at the last 10 tips we've shared in case you missed any or want to revisit them. Thanks for your incredible support for this series--we're so glad it was able to help so many of you navigate your community experience!   Getting Started in the Community An overview of everything you need to know about navigating the community on one page!  Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Community Ranks and YOU Have you ever wondered how your fellow community members ascend the ranks within our community? We explain everything about ranks and how to achieve points so you can climb up in the rankings! Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Powering Up Your Community Profile Your Community User Profile is how the Community knows you--so it's essential that it works the way you need it to! From changing your username to updating contact information, this Knowledge Base Article is your best resource for powering up your profile. Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Community Blogs--A Great Place to Start There's so much you'll discover in the Community Blogs, and we hope you'll check them out today!  Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Unlocking Community Achievements and Earning Badges Across the Communities, you'll see badges on users profile that recognize and reward their engagement and contributions. Check out some details on Community badges--and find out more in the detailed link at the end of the article! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Blogging in the Community Interested in blogging? Everything you need to know on writing blogs in our four communities! Get started blogging across the Power Platform communities today! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Subscriptions & Notifications We don't want you to miss a thing in the community! Read all about how to subscribe to sections of our forums and how to setup your notifications! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Getting Started with Private Messages & Macros Do you want to enhance your communication in the Community and streamline your interactions? One of the best ways to do this is to ensure you are using Private Messaging--and the ever-handy macros that are available to you as a Community member! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Community User Groups Learn everything about being part of, starting, or leading a User Group in the Power Platform Community. Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Update Your Community Profile Today! Keep your community profile up to date which is essential for staying connected and engaged with the community. Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Thank you for being an integral part of our journey.   Here's to many more Tuesday Tips as we pave the way for a brighter, more connected future! As always, watch the News & Announcements for the next set of tips, coming soon!

Hear what's next for the Power Up Program

Hear from Principal Program Manager, Dimpi Gandhi, to discover the latest enhancements to the Microsoft #PowerUpProgram, including a new accelerated video-based curriculum crafted with the expertise of Microsoft MVPs, Rory Neary and Charlie Phipps-Bennett. If you’d like to hear what’s coming next, click the link below to sign up today! https://aka.ms/PowerUp  

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