Variables in Canvas Power Apps | Global and Context

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.

headerImg

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.

  1. Let’s say, I have a Text field called GuestNameField
    guestFieldAdded
    guestNameFieldView
  2. 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.
    formulaWritten
  3. And I have a Label called as DisplayValue which will read from the variable and set it’s text.
    readFromVar
  4. Now, when I run the app and type the name, and when I release the focus on the text box, the
    typeName
    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
    appear

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.

  1. Now, in case of Context Variables, you use UpdateContext({variable: value}) in the formula.
    contextVar
  2. And is accessed/read by simply writing the name of the variable directly
    directContextVarName
  3. And it behaves in the same way, you write something and it can be read
    typeName
    And displayed below as seen in the above Global Variable example
    appear

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
menuItem

Global Variables as marked above can be accessed in their own section
globalStored
And if you open any of those, you can see and even navigate to where they are used.
globalUsage

Similarly, for Context Variables are denoted by the Screen names they are in.
contextStored

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 –

  1. Get N:N records in a Canvas Power App using Common Data Service connector | Power Platform
  2. Adding a Canvas PowerApp to Teams
  3. Correctly connect to an Excel file in a Canvas PowerApp
  4. Sending Image from Canvas PowerApps to SharePoint Document Library using Flows

Thank you!

Advertisement