Archive

Archive for July 18, 2021

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: ,