Archive

Posts Tagged ‘Variables’

Canvas Apps | Optimization | Quick way to find and clean unused variables

Many a times, in Canvas Apps we would have declared variables but no longer used them. In a complex App with substantial screens, its tedious to identify and clean up the unused variables.

In this article, lets learn how to identify unused variables and clean up. If you are unversed with ‘Variables’, refer this article.

On a high level, Set() is used to define Global variable and UpdateContext() or Navigate() are used to define Context variable (i.e., Scope limited to a screen).

To understand unused variables and clean better, I’ve built a simple Canvas App using below steps.

  • Create a new Canvas App from Power Apps portal.
  • On App ‘OnStart’ event, declared 4 Global variables using Set().
  • Add a new Screen with 3 buttons. As buttons Text, used the one of the global variables(i.e.,btnLabel1) declared in App ‘OnStart’. Repeated the same for other 2 buttons.
  • To understand Context variables, on “btn1’s” OnSelect declared 2 context variables using UpdateContext().
  • Now that we got both Global and Context variables, lets understand about variables in-depth.
  • Go to File -> Variables, you will find 2 tabs ‘Global’ and ‘Screen1’ (this is for Screen1 Context variables).
  • Now click on one of the Variables, and you will find 3 tabs:
    • Definitions : Statement where this variable was declared. In our case its App ‘OnStart’.
    • Uses : Where this Variable used.
    • Indirect uses : ¬†Any control indirectly refers the variable. Lets see this in detail in next step.

  • Click on ‘Uses‘, you will find where ‘btnLabel3’ directly referred (i.e., Text of button 3).
  • To understand ‘Indirect uses‘, lets add a new Label and set the Text as “This is indirect label of a Button : ” & btn3.Text“.
  • In the above statement, ‘Button 3’ text was ‘Directly’ set to ‘btnLabel3’ variable, here new Label’s text was set to ‘Button 3’ text, which indirectly referring ‘btnLabel3’ variable.
  • Now, click on ‘Indirect uses‘ of “btnLabel3” variable and you would see the new Label’s text statement.
  • Coming back to the crux of this article, to identify a Unused variable, click on Variable and go to ‘Uses‘ tab. If its blank, you can navigate to the declaration statement from ‘Uses‘ and remove the statement.

ūüôā

Categories: CRM, PowerApps Tags: ,

Power Apps – Understanding ‘Variables’

January 26, 2020 2 comments

If you are a ‘Power Apps’ beginner and wonder how to declare and use variables like the way you do in any of your favorite programming language (C#, Java, PHP, etc..) this article is for you.

Before we jump in to ‘Power Apps’ Variables, lets first understand following things:

  • ‘Power Apps’ is different and works more like Excel.
  • Power Apps and Excel both automatically recalculate formulas as the input data changes, without the need of Variables.
  • In below screen,
    • Sum of A and B is being calculated and set to the Label just by summing up the Text values directly like a formula.
    • If either A or B value changes, Label will recalculate the value.

PA_Var1

Why we need variables in ‘Power Apps’?

  • In general, its recommended to avoid using variables in Power Apps.
  • But sometimes only a variable can enable the experience you want.
  • To understand this, lets tweak the previous screen by adding a Button and Sum up the values on button click.

PA_Var2

  • Here we can’t use formulas (i.e., Like screen 1) to calculate the Sum because its value depends on button Click.
  • We require a variable to hold the Sum before its set to the Variable to display.

Types of Variables:

Power Apps has three types of variables:

PA_Var3

Global Variable:

  • You set the value of the global variable with the¬†Set¬†function.
    • Set( Var_Name, 0 ) sets the global variable¬†to a value of 0.
  • Global variables can hold any value, including strings, numbers, records, and¬†tables
  • In my example, on ‘OnSelect’ of ‘Calculate’ button, using ‘Set’ function, I am setting Sum of txt1.Text and txt2.Text to a Global variable ‘globalVar‘.

PA_Var5

  • And setting the ‘Text’ of ‘Label’ to¬†Global variable ‘globalVar‘.

PA_Var6

  • ¬†Global variable scope is ‘App’ level, can be references from anywhere in the app. Which means, I can read the ‘globalVar‘ from a different screen (i.e., Screen2).

PA_Var7

Context Variable:

You implicitly establish and set context variables by using the UpdateContext or Navigate function.

UpdateContext:

  • UpdateContext( { Var_Name: 0} ) sets the context variable to a value of 0.
  • Context variables can hold any value, including strings, numbers, records, and¬†tables
  • In my example, on ‘OnSelect’ of ‘Calculate’ button, using ‘UpdateContext’ function, I am setting Sum of txt1.Text and txt2.Text to Context variable ‘ctxVar‘.

PA_Var8

  • And setting the ‘Text’ of ‘Label’ to Context variable ‘ctxVar‘.

PA_Var9

  • ‘OnSelect’ of ‘Clear Context Variable’ button, I am setting ‘ctxVar‘ to 0.

PA_Var10

  • Context variable set using ‘UpdateContext’ function’s scope is ‘Screen’ level, can only be references with in the screen.

Navigate:

  • You can also set a context variable when you use the¬†Navigate function to show a screen.
  • In the example below, lets add a new button ‘Navigate to Screen2’ and on ‘OnSelect’, navigate to ‘Screen2’ by passing ‘ctxvar‘ as argument.
    • Navigate(Screen2,ScreenTransition.Fade,{ctxvar:txt1.Text+txt2.Text})

PA_Var11

  • On ‘Screen2’ set the Label’s text to ‘ctxvar’.

PA_Var12

  • Run the application, click on¬†‘Navigate to Screen2’

PA_Var13

  • On the ‘Screen2’ we get ‘ctxvar’ value displayed in label.

PA_Var14

  • Important thing to notice is, Except for¬†Navigate, context variables are limited to the context of a single screen, which is where they get their name. You can’t use or set them outside of this context.

Use Collection:

  • Collection holds a table that is easy to modify.
  • Create and set collections by using the¬†ClearCollect¬†function.
    • You can use the¬†Collect¬†function instead, but it will effectively require another variable instead of replacing the old one.
  • In my example, on ‘OnSelect’ of ‘Add to Collection’ button, collect the ‘txt1.Text’ values to a collection ‘collSum‘ using Collect(collSum,txt1.Text)

PA_Var15

  • We clear the Collection ‘collSum‘ using Clear(collSum) function.

PA_Var16

  • To display the ‘collSum’ values, add a ‘DataTable’ control and set ‘Data source’ to ‘collSum’.

PA_Var17

  • Run the application and keep adding the values. You should see collection values in a table as below.

PA_Var18

Notes:

  • You can see the Variables and Collection on the File menu of your App.

PA_Var19

  • If you give a context variable the same name as a global variable or a collection, the context variable takes precedence. However, you can still reference the global variable or collection if you use the¬†disambiguation operator¬†@[Var_Name].
  • All variables are held in memory while the app runs. After the app closes, the values that the variables held are lost.
  • When the user opens the app, all variables have an initial value of¬†blank.

ūüôā