You are currently viewing PowerApps variables types : context variable vs global variable vs collections

PowerApps variables types : context variable vs global variable vs collections

In this article, we will learn what are the different PowerApps variables types and how to use each type of these variables? In PowerApps, we have different scoped variables as same as the different scopes of variables in the programming language so let’s understand the variables carefully, context variable vs global variable vs collections.

PowerApps variables types

In PowerApps we can find three variable types

  1. the global variable
  2. the context variable
  3. the collections

PowerApps global variable

  • Global variables are Similar to a global variable in programming language
  • Global variables can hold any value, including string, number, record,table etc.
  • Global variables run through the entire App itself it is a solution when you worked with diffrent screens So, we can use global variables within all the screens in our entire Application.
  • Global Variables are created and set by using the Set function, It is not possible to define more than one global variable in only one Set() command, and if you want to set more PowerApps global variables, you must use multiple Set() functions.
  • syntax
Set(variable, value)

  • To store a record in the global variable
Set(variable, {User:"Heba", ID:"123456"})
  • You remove a variable by removing all the Set functions.
  • you cannot delete the Global variables,To clear the global variable, set the variable value using the Blank() function.
Set(GlobalVariable, Blank())

In a single Set() command, you can’t set more than one global variable. You must call the Set() function many times if you need to set numerous global variables.

How do I view variables in power apps studio?

  • To see a list of all global variables in PowerApps File, then click on Variables this would show you all of the app’s global variables.
PowerApps variables types
  • you can click on the variable to display where the variable definition is present and where it is used.
v2 | Power Platform Geeks

Example: PowerApps multiple text inputs. we will learn how PowerApps pass variables to another screen

scenario: I have two screens screen1 and screen2

screen1 has Combobox, date picker, and two buttons

v1 1 | Power Platform Geeks

screen2 has a textbox

v1 2 | Power Platform Geeks

I need when the user clicks on button1 in screen1 it will fill the textbox in screen2 with the value of the Combobox and if the user clicks on button2 it will fill the textbox with the selected date in the date picker.

Solution

  • the best solution in this case is to use the global variable
  • create the global variable using the Set function
  • How do I set a global variable in power apps? the formula for button1 will be
Set(Inputvar1, ComboBox2.Selected.Value)
  • the formula for button2 will be
Set(Inputvar1, Text(DatePicker1.SelectedDate))
  • we use text to convert the value of the datePicker to a text ” PowerApps set variable as text”
  • set the textbox property to be the name of the variable
Inputvar1

this is how powerapps assign variables to fields.

  • now if the user click on button1 in screen1 it will fill the textbox in screen2 with the value of the combobox and if the user click on button2 it will fill the textbox with the selected date in the date picker.

This is how to use PowerApps variable between screens “the global variable”

PowerApps context variable

  • Context variables are similar to the parameters we send to methods in programming languages.
  • Context variables are Great for passing values to a screen.
  • Context variables can be accessed via a single screen.
  • Context variables in PowerApps have a screen-based scope.
  • To create a Context variable we use the UpdateContext function
  • Variables should be wrapped in curly braces.
  • Syntax
UpdateContext({contextvariable: "Example"})
  • To store a record in the Context variable
UpdateContext({contextvariable: {UserName:"Heba", ID:"123456"}})
  • You can create more than one Context variable at a time. A comma separates them within the curly braces. 
  • syntax
UpdateContext({contextvariable1: "value1", contextvariable2: "value2"})
  • For clearing the Context variable, set the variable value using the Blank() function

Clearing a global variable.

UpdateContext({contextvariable, Blank()})
  • You can use Context variables for passing parameters to a screen in the Navigate function.
11 | Power Platform Geeks
passing values to a screen

How do I view variables in power apps studio?

  • To see a list of all Context variables in PowerApps from File, then click on Variables this would show youall the screens that are available in the Application and select the screen to show you where the definition of the variable exists and where it will be used.
PowerApps context variable
PowerApps context variable

PowerApps collections

  • Collections are multi row valued variables used to store tables.
  • The scope of Collections in PowerApps is within the App.
  • Can be saved to the local device for later use
  • You can initialize a collection by using the ClearCollect() function
  • syntax
ClearCollect(CollectionCol, {UserName: "ABC", email:"abc@gmail.com"})

ClearCollect() function will clear the entire content of the collection if it already exists

  • You can use the Collect function instead, but it will effectively require another variable instead of replacing the old one.
  • If you want to add more than one row into your collection, you need to add a comma after the previous row


ClearCollect(CollectionCol, 

{UserName: "ABC", email:"abc@gmail.com"},

{UserName: "DEF", email:"def@gmail.com"}

)

  • Append rows to a collection
    • use the Collect() function.
    • provide the collection name you want to append a row to , then provide the values for the columns.
    • syntax
Collect(
    CollectionCol,
    {UserName: "GHI", email:"ghi@gmail.com"}
)
  • Delete rows from a collection
    • To remove rows from a collection, use the RemoveIf() function.
    • syntax
RemoveIf(CollectionCol,Email="ghi@gmail.com")
  • Clear a collection
    • use the Clear() function
    • syntax
Clear(CollectionCol)

How do I view collections in power apps studio?


To view all collections used within your Power App and their contents, you can go to File > Collections.

v1 3 | Power Platform Geeks
collections

Conclusion

We learned that All variables are held in memory while the app runs. After the app closes, the values that the variables held are lost.

A global variable is a single row variable that exists across all of your Power Apps, allowing you to use it on any screen.

  • powerapps variable types
  • powerapps global variable
  • PowerApps context variable
  • powerapps variables functions
  • powerapps create variable
  • powerapps variable definition
  • powerapps create record variable
  • powerapps set variable
  • powerapps set variable on button click
  • powerapps variable with multiple values
  • powerapps variable clear
  • powerapps delete variable
  • How do I view variables in power apps studio?
See Also

Heba Kamal

Microsoft MVP, MCT, Technical Speaker, Blogger, and Microsoft 365 and Power Platform Consultant.