A variable is a temporary storage that can be defined and used anywhere within Power Apps. Variables are created and typed by default when they are displayed in the functions which define their values.
There are three types of variable are available in Power Apps:
Context variables: The scope of the context variables is at screen level, context variables are limited to the context of a single screen, which is where they get their name. You can’t use or set them outside of this context.
Context variable values can be passed one screen to another screen using Navigate function.
Context variables can hold any value, including strings, numbers, records, and tables.
Syntax: UpdateContext(Context)
Context: A record that specifies the new values for some or all of the context variables in the current screen. For example, UpdateContext({VariableName: Value})
Global variables: The scope of the Global variable is at App level. Global variable can be accessed from anywhere within the app. Unlike local variables, which are limited to specific screen, global variables can be accessed within all the screens in our entire app.
Global variables can hold any value, including strings, numbers, records, and tables.
Syntax: Set(Variable, Value)
Variable: This the name of global variable.
Value: This value is set to global variable.
Collection: The scope of the collection is at App level, a collection is used to store and manage a group of items or records. It acts as an in-memory data source.
It allows you to create, modify, and manipulate data without the need for an external data source. Collections are useful for scenarios where you need make a temporary copy of a data source.
Syntax:Â Collect (Collection, Item,..)
Collection: New or existing collection name.
Item: A record or table to collect, a record will be appended to the collection. For example:
Collect (EmployeeDetails, {EmployeeName:txt_empname.Text, DepartmentName: txt_dptname.Text})
Creating Context variables
Create a blank canvas app as shown below.
Add a blank screen named it Screen1.
Lets, take a Text input control named Textinput1 on Screen1.
Similarly, add Text label and Button control and named them Lable2 and Button1.
Now, we will define the Context variable or local variable which will store the value provided to Text input control on Button’s click.
Now, set button’s OnSelect property to code as shown below.
UpdateContext({Locvar_val: TextInput1.Text})
Here, Locvar_val is the name given to the local variable and TextInput1 is the name of Text input control.
Now, go to Text property of Text label’s control which is Label2 and set to local variable Locvar_val.
So, on the button click value provided to Text input control is stored into local variable Locvar_val and then displayed in Text label control because the Text property of Text label control is set to locvar_val.
Lets run the app, click on Preview the app button.
Now, enter the value to Text input control and click on Button. You can see the value stored in variable is displayed in Text label control.
You can also see the value stored in variable, Click on Variables tab.
Expand Context variables, then expand Screen1 and under that you can see the variable. Click on View Text.
As we know that, the scope of the context variables is within in a single screen, which is where they get their name get.
Now, we will try to access the value of Context variable which is created in Screen1 to another screen.
Let’s add a new screen in canvas app. Click on New screen, select Blank screen and
and named it Screen2.
Now, we have Screen2. Add a Text label control on Screen2, and then set the Text property of Text label to locvar_val. Why we are doing so, Because we want check the whether the value of context variable that we created in screen1 is accessible in screen2 or not.
You can see, the context variable named locvar_val is not accessible in screen2. When you write variable name, power apps showing an error that means it is not available in screen2.
Passing context variable value from one screen to another screen
As we know that, context variable values can be passed from one screen to another screen using Navigate function.
Lets go back to Screen1, here we will pass variable locvar_val value to Screen2 using Navigate function on button click.
Lets add a Button control on Screen1, and set its Text property to code as shown below.
Navigate( Screen2, ScreenTransition.CoverRight, {loc_val: Locvar_val} )
Note that, in this function Screen2 is our target screen, ScreenTransition.CoverRight is a visual transition used for screen navigation, and {loc_val: Locvar_val} is a context record means the value that we want to pass to target screen.
So, we are assigning the context variable Locvar_val value to loc_val during the screen navigation and which will be passed to screen2.
Now, On the Screen2. Let’s set the Text label control property to loc_val.
And you can see, this time we not getting any error.
Now, lets go back to Screen1 and run the app.
Click on Button and you can see it navigates you to Screen2 where you can see the context variable value is displayed on Text label.
You can also directly set value to variables as shown below.
UpdateContext({Locvar_val: "Power Apps"})
Clear the Context variable values
You can clear the Context variables values by setting variable values to blank. Lets, add one Button control and set its Text property to Clear.
Then on the OnSelect property of Button to code as shown below.
UpdateContext({Locvar_val: Blank()})
Now, run the app and click on Button Clear.
Now you can see, context variable value is blank.
Creating Global variables
Global variables can be accessed within all the screens in our entire app.
Lets create a global variable and store the user information to the variable when app is started. Later, we will access the variable value throughout all the screens.
Let’s set the App’s OnStart property to code as shown below.
Set( gvar_UserInfo, User() )
Note that, here gvar_UserInfo is global variable name and User() is the value set to global variable.
User() is an in-built function of Power Apps which holds user information of current user such as Email Address, Full Name and Image.
Let’s access the values of this global variable to Screen1 and Screen2.
On Screen1, add on Text label. Now we will show, User’s full name on this label. So, set it’s Text property to code as shown below.
As global variable store User() function which is an in-built function of Power Apps and it holds user information of current user such as Email Address, Full Name and Image. So that, we can access any information from this function. To access the full name just put dot (.) after global variable name and write FullName.
gvar_UserInfo.FullName
Now go to Screen2, here we will show current user’s email address.
For this add Text label. Set its Text property to code as shown below.
gvar_UserInfo.Email
Now, set the app to Run onStart as shown below.
Now click on Button to navigate on Screen2, and you can see it displays the current users mail address on Text label.
This way, you can access global variable values within all the screens in app.
You can see the values stored in Global Variables.
Click on Variables button, expand Global Variables and select View Record as shown below.
Clear the Global variable values
You can clear the Global variables values by setting variable values to blank.
Below is the syntax:
Set( global_varibale_name, Blank() )
Creating collectionÂ
You can read about Creating a Collection Collection in Power Apps
Also Read..
ClearCollect function in Power Apps
Highlight selected item in gallery
Email validation in Power Apps
Sort Items in gallery Power Apps
Change Item color in gallery based on value