Understand the difference between Set and UpdateContext function in PowerApps - Types of Variables in PowerApps (Global and Local)

Set and UpdateContext function in PowerApps: Understand the difference between Set and UpdateContext function

2 comments

Loading

In this PowerApps Tutorial (Set and UpdateContext function in PowerApps), we will understand the difference between the Set and UpdateContext functions in PowerApps.  We will also get to know about the concept of variables and the types of variables in PowerApps. Like any other programming language like C, C#, Java, .Net, etc, PowerApps do have variables. As we know variables play a vital role in the programming langue while writing complex code – it is needed very much for temporary calculation of data during the programming.

What you will learn from this article (Set and UpdateContext function in PowerApps)?

  • Types of variables in PowerApps
  • How to create variables in PowerApps?
  • What is Set Function in PowerApps and how to use it?
  • How to create global variables in PowerApps using the Set function?
  • Demo1 – How to create an auto-increment number in PowerApps on a button click event using the set function?
  • How to reset controls in PowerApps?
  • What is UpdateContext Function in PowerApps and how to use it?
  • Demo 2: How to create an auto-increment number in PowerApps on a button click event using the update context function?
  • Know about the global vs context variables in PowerApps.
  • Verify the scope of the global and local variables in PowerApps

Types of variables in PowerApps

There are basically two types of variables in PowerApps, namely local and global like any other standard programming language, apart from this there is one more type of variable namely collections.

Let’s have this in the table format.

Variable Type Scope of the Variable Description Function map to the variable
Context Variables Screen This type of variable is known as the “Local” variable. This variable can only be used within the same screen only. UpdateContext
Navigate
Globals App This variable can be used anywhere from the app. Set
Collections App This variable can be used anywhere from the app and it holds the collections object. Collect
Clear
ClearCollect

How to create variables in PowerApps?

We can create the above three types of variables in the below ways:

Set(myGlobalVariable, "My value to global variable") 
Update Context({Local Variable:"My value to local variable"}) 
Collect(myCollectionVariable, "My value to collection variable")

Now, we will understand how to use the above three types of variables.

How to create global variables in PowerApps using the Set function?

As mentioned above global variable can be accessed from all screens in the app, that is the reason the name is global.

Syntax

Set( VariableName, Value )

Explanation

  • VariableName – Required. The name of a global variable to create or update.
  • Value – Required. The value to assign to the context variable.

Demo 1: Auto increment number in PowerApps on button click event using the set function

In this demo, we will learn how to use the global set function in PowerApps and how we can auto-increment the number on the PowerApps button click event (OnSelect).

I am assuming you know how to create a PowerApps Canvas app – so, I am not getting into that, directly going to the demo part.

Here I have created a screen where it has the below controls:

  • A text box to enter the input
  • A label to display running result output.
  • A button to trigger the OnSelect event and do the calculation.
  • A button to trigger the OnSelect event and reset the controls.
Global variables in PowerApps using Set Function
Global variables in PowerApps using Set Function

On the “Click to sum of the Previous Number” button click event adding a set function as below:

Set(varGlobalRunningTotal, varGlobalRunningTotal + txtNumberInput )

Notes:

  • In the above set function, the first parameter is the variable name and the second parameter is the value to the variable.
  • In the next demo, we will ensure the “varGlobalRunningTotal” variable is accessible from another screen due to its global scope availability.

Now, let’s add that variable “varGlobalRunningTotal” to the “lblRunningNumberResult” label’s Text property as shown below:

Auto increment number in PowerApps using the global set function
Autoincrement number in PowerApps using the global set function

Before running the app we will learn how to reset the controls in PowerApps.

How to reset controls in PowerApps?

To reset the controls on the button click the event PowerApps we need to follow the below steps:

  • Select the reset button – here it is named as “Click to Reset the Number”
  • On the “OnSelect” event of this button, add this these function – Set(varGlobalRunningTotal,0);Reset(txtNumberInput);
  • In the above two functions, the set is resetting the global variable value to zero and the second reset function is resetting the text input box.
How to reset controls in PowerApps
How to reset controls in PowerApps?

If you want to attach multiple events (OnSelect) with a single button, then we can add those events by adding a semicolon (;) one after another.

Demo for the concept of the Set function in PowerApps

Now press F5 to preview the app.

Enter any number in the “Enter a number once” textbox, then click on the “Click to the sum of the Previous Number” button, every time the running number gets incremented by the previous resulted number.

Increment a number on button click event in PowerApps
Increment a number on button click event in PowerApps

Now, we will implement the same auto-increment number functionality using the PowerApps update context function whose scope is within the same screen.

Reset PowerApps control on the button click event

Now click on the “Click to Reset the Number” button. The number input text box has been reset to empty and the running number label has been reset as 0 on the Reset button click event.

Reset PowerApps controls on the reset button click event
Reset PowerApps controls on the reset button and click event

Demo 2: Auto increment number in PowerApps on button click event using the update context function

In the second demo, we will implement the same auto-increment using the update context function. We can see we have created the “varLocalRunningTotal” variable, whose scope is to the current screen only – it cannot be accessed from anywhere except the screen where it is created, that’s why the variable name is local.

Syntax

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

Explanation:

  • 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.
difference between Set and UpdateContext function - Auto increment counter in PowerApps
Autoincrement counter in PowerApps using the UpdateContext function

Let’s preview the app again to see the result from the UpdateContext local variable.

Click on the F5 button to preview the app.

This is the same auto-increment number demo as the previous one, the only difference is – here it is used UpdateContext and there it used the Set function.

Auto increment number in PowerApps using the UpdateContext function demo
Autoincrement number in PowerApps using the UpdateContext function demo

Now, we will verify the scope of the local and global variables.

Verify that the global variable created by the set function is accessible from other screens

Now type the “var” word in the Fx box, there we can see two results “varGlobalRunningTotal” and “varLocalRunningTotal”.

The “varGlobalRunningTotal” variable was created in screen 1 using the set function but still, it is accessible from screen 2 as the scope of this variable is an app (global).

The “varLocalRunningTotal” variable is created on screen 2 (current screen) using the UpdateContext function and it is only available on the current screen as the scope of this variable is a screen (local).

PowerApps variables scope verification demo
PowerApps variables scope verification demo

Difference between Set and UpdateContext function – Verify that the local variable created by the UpdateContext function is not accessible from other screens

Go to screen 1 and type the “var” word in the Fx box.

There we cannot see the “varLocalRunningTotal” variable as the scope of this variable was to the particular screen where it was created.

PowerApps UpdateContext local variable is not accessible from other screens
PowerApps UpdateContext local variable is not accessible from other screens

Summary: Difference between Set and UpdateContext function in PowerApps

Thus, in this article, we have learned the following with respect to understanding the various differences between the Set and UpdateContext functions:

  • Variables of various types exist in PowerApps.
  • How to create variables in PowerApps
  • What is a global variable in PowerApps, and how do you create one?
  • What is a local variable in PowerApps, and how do you create one?
  • What is the Set function in PowerApps?
  • How to create global variables in PowerApps using the Set function
  • Demo1: How to create an auto-increment number in PowerApps on a button click event using the set function
  • How do I reset controls in PowerApps?
  • What is the UpdateContext function in PowerApps, and how do you create it?
  • Demo 2: How to create an auto-increment number in PowerApps on a button click event using the update context function
  • I learned about the global vs. context variables in PowerApps.
  • How to read and display the variable value in a PowerApps control
  • What is the collection type variable in PowerApps?
  • Various scopes of the variables in PowerApps

Apart from the Set and Update Context (global and local variables) function, we have one more type of variable named “Collections.” In the next article, I will write about the collection type variables in PowerApps and how to work with them with the PowerApps collection controls. Until then, keep reading and keep learning.

See Also: PowerApps Tutorial

You may also like the below PowerApps articles:

Buy SharePoint Online & Office 365 Administration eBook

Buy the premium version of SharePoint Online & Office 365 administration eBook from here:


Buy SharePoint Online & Office 365 Administration eBook

Reference URL

About Post Author

2 comments on “Set and UpdateContext function in PowerApps: Understand the difference between Set and UpdateContext function”

Do you have a better solution or question on this topic? Please leave a comment