For all newbies, being able to use Variables is one the most curious questions put out immediately. Here’s a simplistic example of using Variables, both Global and Contextual in this post.
Global Variable
Global Variables as the name suggest, run through the entire App itself and can be changed and altered when needed.
Global variables can hold boolean, string, number etc.
- Let’s say, I have a Text field called GuestNameField
- Global Variables are created and set by using Set(variable, value) function.Now, on the OnChange event of the GuestNameField above, I’m setting the variable by the formula Set(guestName, GuestNameField.Text). Where, guestName is the field I created on the fly and I’m setting the text from the GuestNameField textbox whenever the Textbox will trigger OnChange Event.
- And I have a Label called as DisplayValue which will read from the variable and set it’s text.
- Now, when I run the app and type the name, and when I release the focus on the text box, the
And when the focus from the TextBox is released, OnChange of the TextBox will trigger and the Label will display the text written since it’s reading the value from the variable
Context Variable
Now, a variation to the Context Variable from the Global Variable is that the Context variable have the scope of the only the current screen they are declared in. Navigate function is also a form of Context variable in which case, you can pass values to other screens.
Context variables can hold strings, numbers, records and tables.
- Now, in case of Context Variables, you use UpdateContext({variable: value}) in the formula.
- And is accessed/read by simply writing the name of the variable directly
- And it behaves in the same way, you write something and it can be read
And displayed below as seen in the above Global Variable example
Checking your Variables
In case you’re wondering you’ll lose track of your variables, you can check the File menu and access the Variables section as shown below
Global Variables as marked above can be accessed in their own section
And if you open any of those, you can see and even navigate to where they are used.
Similarly, for Context Variables are denoted by the Screen names they are in.
I tried to summarize this on a high level. You can refer this Microsoft Documentation for details Understand canvas-app variables in Power Apps.
Hope this was easy!
In case you are looking for more post related to Canvas PowerApps, here you go –
- Get N:N records in a Canvas Power App using Common Data Service connector | Power Platform
- Adding a Canvas PowerApp to Teams
- Correctly connect to an Excel file in a Canvas PowerApp
- Sending Image from Canvas PowerApps to SharePoint Document Library using Flows
Thank you!