How I Handle Variables in Power Apps: My Tips for Set, UpdateContext, and With.

I’m often asked about the use of different variables in the Power Apps canvas application, so I’ll note the use I make of them.

There are three types of variables in Power Apps

In Power Apps, there are three types of variables
*This can be rephrased to say that there are three types of scopes.

  • Set / Collection
  • UpdateContext
  • With

Each variable has a different scope (the range within which the variable can be accessed), so it is necessary to use them differently depending on the situation.

Set / Collection

First, consider global variables declared with “Set” and collections declared with “ClearCollect” and so on.

The scope of these variables is “in-app” and can be referenced or modified from anywhere.

This is convenient because they can be referenced from anywhere, but because they can be changed from anywhere, they can cause degreasing when modifying the app, and they can confuse other developers when handing over a built app to another developer.

I mostly use it to define constants like this,

Using global variables may make the application run faster, and there are many cases where the entire application needs to manage values (e.g., shopping cart, side menu open status, etc.), so I think it is better to use them only when necessary.

Global variables can also be accessed from some custom components

Incidentally, global variables declared with Set, etc., can also be accessed by custom components with access aprescope turned on.

UpdateContext

Next, let’s consider the context variables declared in the UpdateContext function. The scope of variables declared in “UpdateContext” is limited to “within the screen where the variable was declared.

Often used to show/hide pop-ups, etc.

I often see articles stating that UpdateContext should be used as much as possible because it has a narrower scope than a variable declared with Set, and I use UpdateContext as much as possible as well.

Whether to use collections or table-type context variables

For example, when we want to retrieve multiple items from a data source such as an SPO list and hold them in a variable, we often see source code that uses and holds a collection as shown in the image.
However, if there is no need to add or remove items to or from the collection after this process, we believe it is better to use a table-type context variable.

Declaring a variable in a collection also makes it accessible from anywhere in the application, so if there is no particular reason to use a collection, I personally recommend using a table-type context variable.

Context variable to be used as an argument to the Navigate function

However, a little caution must be exercised because the Navigate function can also rewrite the context variable of the screen to which it transitions.

If context variables passed from other screens as arguments are edited/used at various points in the screen, unexpected bugs may occur when describing processing with screen transitions.

* In many cases, Power Apps coding conventions state that variables declared in UpdateContext should be named something like “loc~~~,” but context variables as arguments passed from other screens can be named “param~~~ (prm~~~)” or “arg~~~”. which are passed from other screens, makes the source easier to read.
Just my opinion.

Adding the prefix “param” to the name of the context variable, as shown in the image,

makes it easier to know which variable to pass the value to when calling it from another screen.

With

Finally, consider the variables that are declared in the first argument of the With function. This variable is the narrowest in scope and can only be referenced within the With function.

Although it is not well known to exist, I personally really like this With function, which holds values with a moderate scope.

For example, consider a process that checks the return value of the Patch function, updates the form if it succeeds, and displays a message if an error occurs.
In this case, the UpdateContext function is unnecessary and can be implemented with the With function only.

The official Microsoft website also recommends declaring variables with the With function rather than context variables or global variables, so I personally try to use this With function as much as possible.

Using With is preferred over context or global variables as it is self contained, easy to understand, and can be used in any declarative formula context.
Excerpt from MS Official

Summary of this article

Finally, to summarize the contents of this article, we believe the following order should be considered when using variables
1. Consider using the With function.
2. If it is difficult to achieve with the With function, consider using the UpdateContext function.
3. If neither of these is feasible, use the Set function

The tricky part of taking over a Power Apps canvas app is the variables.

The recent addition of “Find” and “Replace” functions has made Power Apps much more readable, but I think that if you write your code as neatly as possible, your successor developers will appreciate it.

Related Articles

コメント

Copied title and URL