Function Friday – Working with Update Context

Introduction
Continuing with our Function Friday series, like last week’s ‘Patch’ function, we will investigate how to save a temporary create and store a piece of information known as a variable with the Update Context function.
There can be many times when as a developer you will need to be able to store values within your application for use later on, either to set a component’s state to either true or false i.e. it is visible or it isn’t, or a string or integer value.
There are several ways to do this in Power Apps. You have the option of Setting, with, or Updating Context. In this post we will look at the Update Context function, what it does, what to consider, and when to use it.
UpdateContext({locVariableName: "Some Value"})
What it does.
In a nutshell, Update Context stores a variable, that can then be used in the app to control other components or store values from inputs. There are several options for creating and updating variables within Power Apps, these are Set, With and Update Context. So what are the differences? Well, that comes down to the scope of what you’re trying to do.
Scope
When deciding which type of function to use when storing variables it’s important to consider the scope of use for that variable. Update Contexts scope is a local one, meaning that if you set this on one screen and then navigate to another your variable will be lost, there is a way to move local variables from one screen to another, which I will cover later, though if you are having to do this multiple times, it might be worth considering another function like ‘Set’.
For example, if you have certain components on a screen, you want to be visible only when the user clicks a button then Update Context, can achieve this, and then reset itself when the user navigates away from that screen.
Example
UpdateContext( { locShowProgressSpinner: true, locSubmitBtnDisplayMode: DisplayMode.Disabled, locCount: 0 } )
We can set multiple local variables in a single Update Context. However, Update Context is not limited to static values, we can also use dynamic values. For example in the code below, we create a local variable, in which when the user clicks on a button a flow is triggered, we can use Update Context to store the returned values from the flow.
UpdateContext({locFlowResults: testFlow.Run()});
To access these in the application we need to add a ‘.’ after the variable name. For example:
locFlowResults.FlowResult
Creating context during navigation
One bonus bit of knowledge for you all, did you know, you can define a local variable when navigating between screens? You can set the context in the Navigation function (which we will cover in a later article).
Navigate(Target Screen Name, Transition Type, {locVariable: Value})
If you have a variable that you want to use on multiple screens across your canvas app, you might want to think about whether Update Context is the right variable type to use.
Conclusion
In a nutshell, getting the hang of the Update Context function in Power Apps can make a world of difference for your app development. It’s like having a handy toolbox for managing local variables, helping you create smooth and interactive user experiences. Whether you’re toggling component visibility or storing dynamic values from flows, Update Context is your go-to for local state management. Just remember to think about the bigger picture and whether Update Context or another function like ‘Set’ suits your needs best.
Thanks for joining us for this week’s Function Friday! We hope you found it helpful. Don’t miss out next time when we explore the Navigation function and how to keep everything running smoothly as you move between screens. Happy coding, and see you next Friday!
Leave a comment