Table Of Contents:
• Variable Scope
• Usage Examples
• Why Use Global And Local Variables?
Variable Scope
A variable’s scope determines where it can be referenced in the app. If the variable is required on multiple screens use a global variable. Otherwise, use a local or context variable instead. Choose the proper variable type by determining its scope.
Variable Type | Declaration Method | Scope |
Global | Set Function | Variable is available across all app screens |
Local | UpdateContext Function | Variable is only available on a single app screen |
One-time | With Function | Variable is only available inside the WITH function |
Usage Examples
Global Variable (SET function)
Set(
gblSalesTaxAmount,
Value(txt_OrderForm_SubtotalAmount.Text) * 0.13
);
Local Variable (UPDATECONTEXT function)
UpdateContext(
{
locLineItemsCount: 0,
locShowConfirmationMenu: false,
locOrderFormMode=Blank()
}
);
One-Time Variable (WITH function)
With(
{varBusinessContact: LookUp('Sales Orders', ID=ThisItem.ID)},
Concatenate(
varBusinessContact.FirstName,
" ",
varBusinessContact.LastName
);
Why Use Global And Local Variables?
Imagine a large canvas app with many screens that only uses global variables. When updating a variable’s value the developer must be aware of its impact across all screens. If the developer does not correctly determine how to use a variable there are unintended consequences (i.e. a software bug).
Local variables can only be used on one screen. Developers have an easier time assessing the impact to a single screen as opposed to many screens. Higher quality code can be written at a faster pace.
One-time variables are not persistently stored in memory. After the With function is executed the variable is cleared from memory and cannot be accessed outside of the function.
Did You Enjoy This Article? 😺
Subscribe to get new Power Apps articles sent to your inbox each week for FREE
Questions?
If you have any questions about Power Apps Standards: Variable Types please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.
I’m a citizen developer and I’m really developing for the first time with Power Platform. Though I understand perfectly what a local variable is, I absolutely do not see the point of using them.
If I have a variable that I use throughout the app, global variables are mandatory.
If it happens that I’ll only use them in one screen, global variables also work.
I can set all my global variables in the OnStart property, once for all, with a Concurrent() fonction, and proper comments, whereas I have to use the Find feature to find all the « update context » formulas in order to spot the control where this or that local variable has been created.
Of course, every variable that is set as a global variable and used in a single screen has to carry its own name, which can be easily done by adding the screen name to the variable itself.
So, as far as I’m concerned, I really do not see any use, whatsoever, to bother with local variables.
Any thoughts on this Matthew ?
Thanks.
Altide,
Did you read the section called “Why Use Global And Local Variables?” It explains my thinking on the subject.
Example:
Imagine your canvas app has a screen with a pop-up menu whose visibility is controlled by the variable locShowPopUpMenu (true/false). It becomes visible when triggered by some event and is hidden when the user leaves the screen.
Now you want to copy the pop-up menu and all of its functionality to another screen. When you copy the variable locShowPopUpMenu to another screen and work with it there you don’t have to be worried about its impact on the first screen
Hi Matt, thank you for this useful guide. In the global variable example you have prefixed with var. In your naming convention guide you suggest prefixing with gbl to indicate scope.
Dean,
Congrats. You are the first person to report an issue. I credited you on the change log.
Link: https://www.matthewdevaney.com/power-apps-coding-standards-for-canvas-apps/change-log-for-power-apps-coding-standards/
Hi!
Great entry on a great site! I think parameters passed to Screens should be mentioned as they mostly behave like local variables. What would you suggest as a naming convention for these?
Thx
Mihaly,
Naming conventions is a part of the guide you are reading. Here is the link:
https://www.matthewdevaney.com/power-apps-coding-standards-for-canvas-apps/power-apps-standards-naming-conventions/
Yes, I did read that one. But there is nothing on parameters passed to Screens with Navigate(). Shall we also prefix these with ‘loc’?
**This is more of a mini Rant… and certainly not directed to you Matthew**
I see the value in using local variables; however, I really find the syntax to using them deters me from using them.
Defining/Updating a global variable is pretty compact:
Set(VariableName, VariableValue)
Defining/Updating a local variable has a much more cumbersome syntax:
UpdateContext({VariableName: VariableValue})
I just don’t get what the need was for the additional complexity. Why not follow the same basic syntax used for setting Global variables, ie. “SetLocal(VariableName, VariableValue)”… less wordy, no curly parenthesis required, more compact!
And for this reason, I typically do as Altide does and keep to Global Variables prefaces by screen name.
**mini Rant over** :o\
I’m sure there must be a legitimate reason… anyone know of one?
Ken,
I’d always assumed local variables were like that because the Navigate function needed a way to pass them to a screen. But I don’t know for sure. The lack of consistency between globals and locals is unfortunate, I agree.
Navigate(‘Screen Name’, ScreenTransition.None, {locVar1: true, locVar2: true});