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

9 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Altide Didier
Altide Didier
1 year 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
1 year 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.

Mihaly Mikesy
Mihaly Mikesy
1 year ago

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 Mikesy
Mihaly Mikesy
1 year ago

Yes, I did read that one. But there is nothing on parameters passed to Screens with Navigate(). Shall we also prefix these with ‘loc’?

Ken Martell
Ken Martell
6 months ago

**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?

Last edited 6 months ago by Ken Martell