Power Apps Standards: Variable Types

Power Apps Standards: Variable Types
Table Of Contents:
• Variable ScopeUsage ExamplesWhy 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 TypeDeclaration MethodScope
GlobalSet FunctionVariable is available across all app screens
LocalUpdateContext FunctionVariable is only available on a single app screen
One-timeWith FunctionVariable 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.





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.

Subscribe
Notify of
guest
4 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Altide Didier
Altide Didier
1 month ago

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.

Dean
Dean
3 days ago

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.