Skip to main content
Valto — Keep ahead of tomorrow

News

Using The UpdateContext Function In Power Apps

Discover how to efficiently use the UpdateContext function in Power Apps to manage context variables, optimise memory usage, and enhance app performance. Learn best practices and practical examples for Canvas Apps.

PowerApps
Published
21 January 2025

Using The UpdateContext Function In Power Apps

For any none trivial application you will need to manage variables used to store and manipulating data within your app.

In this blog post, we’ll focus on the UpdateContext function, which is used to create and update context variables in a Canvas App.

What is the UpdateContext Function?

The UpdateContext function is used to create or update context variables – which are defined as temporary variables that are scoped to a single screen, meaning they cannot be accessed from other screens within the app.

This makes them ideal for storing data that is only relevant to a specific part of your application or where you need a limited variable lifespan.

Syntax and Usage

To use the UpdateContext function, you pass a single record that specifies the name of the variable and the value you want to assign to it, this will yield one of 2 behaviours

  • If the variable already exists, its value will be updated
  • If it doesn’t, a new variable will be created.

Here is the basic syntax:

UpdateContext({ VariableName: Value })

For example, to create a context variable named ShowLoader and set its value to true, you would use the following formula:

UpdateContext({ ShowLoader : true })

This context variable can then be used to make your application dynamic and respond to user interactions. In our example we can use this to Toggle visibility of a loading message to the user.

two people in a meeting with a laptop

When to Use UpdateContext

Its important to call out the differences between the Set and UpdateContext functions and this comes down to the scope of the variable you need.

Use the Set function for global variables that need to be accessible across the entire app. Use UpdateContext for variables that are only needed on a single screen and information does not need to be persisted beyond that page.

Best Practices

1. Memory Efficiency: Context variables are more memory-efficient because they are cleared when the user leaves the screen, Thus reducing the resources needed by the app and ensuring the app is only taking up the memory it needs.

2. Simplify Your App: Using context variables can make your app easier to understand and maintain by keeping the scope of variables limited to where they are needed, thus prompting the developer to create a clear separation of concerns within the application.

3. Avoid Overuse: Like anything too much of a good thing can be bad and that’s the case of context variables. If used in the wrong situation this can lead to a lot of repetition or added complexity to the app if implemented poorly. Actively review if Context or Global variable is

Practical Example

Let’s consider a practical example where you want to display the loading message when a button is clicked. You can use the UpdateContext function to manage the visibility of the dialog box.

1. Create a Button: Add a button control to your screen and set its OnSelect property to the following formula:
UpdateContext({ ShowLoader: true })

2. Create a Loader Message: Add a container control to represent the loader and set its Visible property to ShowLoader.

3. Hide the Message: Add a hide button on the loading screen (to simulate completion of the loading event) and set its OnSelect property to:
UpdateContext({ ShowLoader: false })

This setup ensures that the loader is only visible when the ShowLoader variable is true, and it can be closed by setting the variable to false.

Additional Details about Context Variables

Context variables can hold various types of information, including single values, records, tables, object references, and any result from a formula.

This flexibility allows you to use context variables for a wide range of purposes within your app.

There is no explicit declaration required of a context variable as UpdateContext handle declaration and assignation in a single operation.

If you remove all references to a context variable, it will cease to exist. To clear a variable, you can set its value to the result of the Blank function.

You can view your variables’ values, definitions, and uses with the Variables view under the File menu in the authoring environment. This feature helps you keep track of your variables and manage them effectively.

Using The UpdateContext Function In Power Apps - Our Expert Says

The UpdateContext function is an invaluable tool for managing temporary data within Power Apps, particularly when working with Canvas Apps. By limiting the scope of variables to a single screen, it helps maintain app efficiency and keeps the app clean and easy to manage. In my experience, it’s crucial to use this function for tasks that don’t require data persistence across multiple screens. For instance, when you only need data on a specific screen—like managing the visibility of a loader or a form—UpdateContext is a great choice. Just remember, overuse can lead to complexity, so always assess whether a global variable with Set might be more suitable for your app’s needs. The key takeaway here is to think about scope and memory efficiency when choosing between UpdateContext and Set.

Trusted by Leading Brands

“We work with organisations across many sectors of different shapes and sizes from 10 to 100,000 employees.”

Contact Form

Contact our team today!

Talk to our team

Contact Us

Topics
Share this

Related reading

NewsMicrosoft 365

Dynamics 365 vs Model Driven Power Apps

Explore how SMEs can use Microsoft Model-Driven Apps to create affordable, custom CRM-like systems. Learn how to manage customer data, track sales, automate tasks, and access powerful reporting—without the high cost of Dynamics 365.

22 January 2025

NewsPowerApps

Power Apps vs. Power Automate

Explore the differences between Power Apps and Power Automate and learn how combining them can deliver powerful, low-code solutions for businesses. Discover how these tools work together to streamline processes and improve efficiency.

21 January 2025

NewsPowerApps

Power Apps Date & Time Functions

Learn how to use Power Apps’ Date & Time functions to manipulate, calculate, and format date and time values in your applications. This blog includes practical examples and expert advice, helping you unlock the potential of these essential tools in Power Apps.

21 January 2025