How & When To Use PowerApps UpdateContext vs Set Function

PowerApps UpdateContext vs Set Variable function

There are two functions that define variables: The Set function and the UpdateContext function. You might ask yourself, when to use the Set Function versus when to use the UpdateContext function.

Note: Besides the variables defined by the Set and UpdateContext function, there are also PowerApps collections. We describe how to work with collections in a dedicated article about PowerApps collections.

What is the difference between PowerApps Set and UpdateContext function?

  • The Set function defines a global variable (application wide accessible) .
  • The UpdateContext function defines a local variable (only accessible within its screen). UpdateContext variables values must be defined in the JSON format.

PowerApps Set Function

The Set function lets you define a named variable that is globally accessible within the app.

Syntax

Set( VariableName, Value )

Input Parameters

  • VariableName (mandatory): The name of the variable you want to set.
  • Value (mandatory): The value of the variable which can be for instance a string, a number, a boolean, a record or table.

Examples: PowerApps Set Function

Set(name, "John Doe");
Set(isMale, true);
Set(age,34);
Set(
    person,
    {
        firstname: "John",
        lastname: "Doe",
        age: 34
    }
);

After the execution of this code, you can find these variables via View menu > Variables > Global.

powerapps set function example result
PowerApps Global Variables under View menu > Variables > Global

PowerApps UpdateContext Function

The UpdateContext function in PowerApps lets you define a screen specific variable within the JSON format.

Syntax

UpdateContext( UpdateRecord )

Input Parameters

  • UpdateRecord (mandatory): The record you want to store in JSON format.

Examples: PowerApps Set Function

UpdateContext({name: "John Doe"});
UpdateContext({isMale: true});
UpdateContext({age: 34});
UpdateContext(  
    {
        person:
        {
            firstname: "John",
            lastname: "Doe",

            age: 34
        }
    }
);

After the execution of this code, you can find these variables via View menu > Variables > ScreenName.

powerapps updatecontext function example result

PowerApps Screen Variables under View menu > Variables > <ScreenName>

There is another way to create a variable for a screen. When you call the Navigate function you can optionally pass a variable to the next screen. See the example below.

Navigate(MyScreen,ScreenTransition.None,{name: "Joe"});

// Creates the variable name within the screen MyScreen 

When to use Set and when to use UpdateContext?

  • Use the Set function whenever your variable is needed within multiple Screens. Since UpdateContext are only accessible within in screens, within the App.onStart() event UpdateContext is not possible. You have to use Set.
  • Use UpdateContext whenever possible. Keeping data as locally as possible is generally good practice within software architecture. Global variables might cause unexpected effects. Just image to set a global variable in different contexts on different screens.

What if Set and UpdateContext are used for the same variable?

A little riddle at the end, what is the value of myVar after the following code?

UpdateContext({myVar: "UpdateContext"});
Set(myVar, "Set");

Within the screen it is “UpdateContext”. Everywhere else it is “Set”.

Leave a Comment

Your email address will not be published. Required fields are marked *