Variables in Power Apps
Variables can be used in Power apps. Variables allow us to store information that we need to reference in our app temporarily. Variables are temporary and only available to the current user in their current session. When the user closes Power Apps, all the information stored in variables is no longer available. If we need to store information for use later or by other users, then we will need to write that information to a data source.
The Power Apps has three different types of variables. 1. Global variables We use the Set function to create and set the value of a global variable. After we set the variable, we can reference its value or update its values throughout our app. This allows us to avoid repetitive query for the same information repetitively which increases the performance of the app.
Note: Global variables are available throughout the app on all screens. There are also context variables which are scoped to a single screen.
Power Apps Syntax Set(VariableName, Value )
The function has the following parameters:
- VariableName - Required. The name of a global variable to create or update.
- Value - Required. The value to assign to the global variable.
The Global variable can hold a number, text string, Boolean, record, table, etc. that can be referenced from anywhere in the app.
Examples: a) Set(myVariable, 45 ) The function creates or modifies the global variable myVariable, setting its value to 45, which can be referenced anywhere in the app. b) Set(myVariable, myVariable + 1 ) The variable can refer itself. The function increments the value of the myVariable global variable from the previous example to 46. c) Set(Name, "Ashish" ) The function creates or modifies the global variable Name setting its value to Ashish, which can be referenced anywhere in the app.
4. Set( Person, { Name: "Ashish", City: "Sonipat" } ) It creates or modifies the global variable Person, setting its value to a record. The record contains two columns, named Name and City. The value of the Name column is Ashish, and the value of the City column is Sonipat.
The Person variable has the value of record {Name: "Ashish", City: "Sonipat"}.
The record can be reference anywhere in the app, the whole record can be referenced by the variable Person, or we can also reference an individual column of the record with Person.Name or Person.City , i.e, RecordVariable.ColumnName.
Add a gallery control, and put the Items property to the Person variable. Add Text label controls and bind it to their corresponding columns.
Click on Update 1 button.
e) Set( Person, Patch( Person, {City: "Panipat" } ) ) By the help of Patch function, we can update the Person global variable by setting the value of the City column to Panipat.
The Person variable now has the value of record { Name: "Ashish", City: "Panipat" }.
Click on Update 2 button.
We can see in the following image that the City column value is updated.
Collections A collection is a special type of variable for storing a table of data. We can create the collection manually or by loading another data sources table into it. Collections are available throughout our app, like global variables, and they are created using the Collect or ClearCollect function.
Collection holds a table that can be referenced from anywhere in the app. Allows the contents of the table to be modified rather than being set as a whole. Can be saved to the local device for later use.
2. Context variables The Context variables are like Global variables except they are only referenced on the screen where we create them.
We can create the Context variable by using the UpdateContext or Navigate function.
Default value of a Variable It is important to remember that the default value of a variable will vary based on the variable type if you do not set the default property.
- Text variables are ""
- Number variables are 0
- Boolean variables are false
Reading variables in Power Apps In this exercise, we will understand to read the variables in the power apps.
Step 1: Add two Button controls and one Text label control on one screen. Set the formula Set(Name, “Ashish”) on the OnSelect property of Button 1.
Step 2: Set the formula UpdateContext({Name: "Anjali"}) on the OnSelect property of Button 2.
Step 3: Hold the Alt key and click on both the buttons to initialize the variable Name.
Step 4: Now set the Text property of the label control to the variable, Name.
We can note that we see the name Anjali in the label control.
If we use the same name for a context variable as we do for a global variable or a collection, the context variable will be given priority or take precedence. This means that when we refer to that variable in our code, the context variable's value will be used instead of the global variable or the collection's value. The context variable "overrides" or "takes over" the same-named global variable or collection within the specific context where it is defined. However, we can still reference the global variable or collection if we use the disambiguation operator [@Name].
Step 5: Add another Text label control and set its Text property to [@Name].