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.

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);