April 22, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

Power Apps Set vs UpdateContext

In today’s article, I will go back to the basics and try to explain in a simple way the differences between Set and UpdateContext functions in PowerApps.

Basically, we will need to think about the scope of the variable we will be using.

Global variables

These are variables accessible via all the screens of your app, and will live for the entire duration of the users’ sessions, hence the name “Global”. We can say, they have an app’s wide scope.

Local variables

In contrary to global variables, local ones, are local to the screen using them, and they need to be declared in a Json format.

 

Set Function

Set( VariableName, Value )

As per Power Fx documentation –> https://learn.microsoft.com/en-us/power-platform/power-fx/reference/function-set

Use the Set function to set the value of a global 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.

Global variables are available throughout your app on all screens. These are the simplest kind of variables and fill the needs of most situations. There are also context variables which are scoped to a single screen and collections that allow row level modifications to tables.

You can set a global variable to a :

  • String
  • Number
  • Record or Object
  • Table …etc.
Set(name, "Mike Doe");
Set(isAdmin, true);
Set(points,77);
Set(
    person,
    {
        firstname: "Mike",
        lastname: "Doe",
        age: 31
    }
);

 

UpdateContext Function

UpdateContext( UpdateRecord )

 

The Power Fx documentation related to UpdateContext is available at –> https://learn.microsoft.com/en-us/power-platform/power-fx/reference/function-updatecontext

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.

Similar to Set function, you can set a global variable to a :

  • String
  • Number
  • Record or Object
  • Table …etc.

 

UpdateContext({name: "Mike Doe"});
UpdateContext({isAdmin: true});
UpdateContext({points: 77});
UpdateContext(  
    {
        person:
        {
            firstname: "Mike",
            lastname: "Doe",

            age: 31
        }
    }
);

Conclusion

It is mainly about the scope of the variable, so please, if a variable is meant to live in just one screen, use UpdateContext, otherwise, Set is the one to pick.

In today’s article, I will go back to the basics and try to explain in a simple way the differences between Set and UpdateContext functions in PowerApps.

Basically, we will need to think about the scope of the variable we will be using.

Global variables

These are variables accessible via all the screens of your app, and will live for the entire duration of the users’ sessions, hence the name “Global”. We can say, they have an app’s wide scope.

Local variables

In contrary to global variables, local ones, are local to the screen using them, and they need to be declared in a Json format.

 

Set Function

Set( VariableName, Value )

As per Power Fx documentation –> https://learn.microsoft.com/en-us/power-platform/power-fx/reference/function-set

Use the Set function to set the value of a global 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.

Global variables are available throughout your app on all screens. These are the simplest kind of variables and fill the needs of most situations. There are also context variables which are scoped to a single screen and collections that allow row level modifications to tables.

You can set a global variable to a :

  • String
  • Number
  • Record or Object
  • Table …etc.
Set(name, "Mike Doe");
Set(isAdmin, true);
Set(points,77);
Set(
    person,
    {
        firstname: "Mike",
        lastname: "Doe",
        age: 31
    }
);

 

UpdateContext Function

UpdateContext( UpdateRecord )

 

The Power Fx documentation related to UpdateContext is available at –> https://learn.microsoft.com/en-us/power-platform/power-fx/reference/function-updatecontext

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.

Similar to Set function, you can set a global variable to a :

  • String
  • Number
  • Record or Object
  • Table …etc.

 

UpdateContext({name: "Mike Doe"});
UpdateContext({isAdmin: true});
UpdateContext({points: 77});
UpdateContext(  
    {
        person:
        {
            firstname: "Mike",
            lastname: "Doe",

            age: 31
        }
    }
);

Conclusion

It is mainly about the scope of the variable, so please, if a variable is meant to live in just one screen, use UpdateContext, otherwise, Set is the one to pick.