top of page

UpdateContext function in Power Apps

Use the UpdateContext function to create a context variable, which temporarily holds a piece of information, such as the number of times the user has selected a button or the result of a data operation.

Context variables are scoped to a screen, which means that you can't build a formula that refers to a context variable on another screen. If you've used another programming tool, you can think of a context variable as similar to a local variable. Use the Set function to work with global variables that are available throughout your app.


Power Apps are based on formulas that automatically recalculate as the user interacts with an app. Context variables don't offer this benefit and can make your app harder to create and understand. Before you use a context variable, review working with variables.


To create or update a context variable, pass a single record to the UpdateContext function. In each record, specify the name of a column, which defines or matches the name of the variable, and the value to which you want to set that variable.

  • If you specify the name of a variable that you've previously defined, UpdateContext sets the value of the variable to the value that you specify.

  • If you specify the name of a variable that doesn't yet exist, UpdateContext creates a variable with that name and sets the value of that variable to the value that you specify.

  • If you've previously defined a variable but don't specify it in this particular UpdateContext formula, its value remains the same.

Context variables are implicitly created by using the UpdateContext or Navigate function. There is no explicit declaration required. If you remove all the UpdateContext and Navigate references to a context variable, then that context variable will cease to exist. To clear a variable set its value to the result of the Blank function.


You can see your variables' values, definitions, and uses with the Variables view under the File menu in the authoring environment.


You reference a context variable in a formula by using the variable's column name. For example, UpdateContext( { ShowLogo: true } ) creates a context variable named ShowLogo and sets its value to true. You can then use the value of this context variable by using the name ShowLogo in a formula. You can write ShowLogo as the formula for the Visible property of an image control and show or hide that control based on whether the value of the context variable is true or false.


As the examples later in this topic show, context variables can hold several kinds of information, including these:

  • a single value

  • a record

  • a table

  • an object reference

  • any result from a formula

A context variable holds its value until the app is closed. If you define a context variable and set its value on a particular screen, that information remains intact even if the user switches to a different screen. Once the app is closed, the context variable's value will be lost and must be recreated when the app is loaded again.


Every context variable is scoped to a screen. If you want to define a context variable on one screen and modify that variable from another screen, you must build a formula that's based on the Navigate function. Or use a global variable.

UpdateContext has no return value, and you can use it only within a behavior formula.


Syntax

UpdateContext( UpdateRecord )

  • UpdateRecord – Required. A record that contains the name of at least one column and a value for that column. A context variable is created or updated for each column and value that you specify.

UpdateContext( { ContextVariable1: Value1 [, ContextVariable2: Value2 [, ... ] ] } )

  • ContextVariable1 - Required. The name of a context variable to create or update.

  • Value1 - Required. The value to assign to the context variable.

  • ContextVariable2: Value2, ... - Optional. Additional context variables to create or update and their values.

Examples


Step-by-step example 1

  1. Name the default screen Source, add another screen, and name it Target.

  2. On the Source screen, add two buttons, and set their Text properties so that one says English and the other says Spanish.

  3. Set the OnSelect property of the English button to this expression: Navigate(Target, ScreenTransition.Fade, {Language:"English"})

  4. Set the OnSelect property of the Spanish button to this expression: Navigate(Target, ScreenTransition.Fade, {Language:"Spanish"})

  5. On the Target screen, add a label, and set its Text property to this expression: If(Language="English", "Hello!", "Hola!")

  6. On the Target screen, select Shapes on the Insert tab, and then select the Back arrow.

  7. Set the Back arrow's OnSelect property to this formula: Navigate(Source, ScreenTransition.Fade)

  8. From the Source screen, press F5, and then select the button for either language. On the Target screen, the label appears in the language that corresponds to the button that you selected.

  9. Select the Back arrow to return to the Source screen, and then select the button for the other language. On the Target screen, the label appears in the language that corresponds to the button that you selected.

  10. Press Esc to return to the default workspace.


Step-by-step example 2

  1. Open the canvas app where you want to use this formula.

  2. Add a new blank screen by selecting New screen from the command bar.

  3. Add a button, and set its OnSelect property to this formula: UpdateContext( { Name: "Lily", Score: 10 } )


Watch Talks Academy with Aroh Shukla on Power Apps - Session 2

The Tech Platform


Source: Microsoft

0 comments
bottom of page