After my introduction post about How to develop your apps in PowerApps. Today a second post about the developing PowerApps basics as it seems to be difficult to get started with Context variables in PowerApps.

I’m going to start with a few simple buttons and a label in PowerApps.

I’m trying to make the reset button set the value in the box to 0 and the Add 1 button will increase the value in the blue box by 1.

I could probably do this without using a Context variable and update the value of the box that Iv’e got. However for this example I’m gpoing to update context variable.

To update a variable UpdateContext will help you. But before you can update a variable you will need to create a new variable called MyVariable you could use this example.

Set(MyVariable, 0)

The above will create you a global variable that can be used across screens. If you are after a variable that can be used within your current screen only then you can use UpdateContext instead:

UpdateContext({MyVariable: 0})

After you have done this you can now use UpdateContext to update the variable. The Set also sets the variable to 0.

UpdateContext({MyVariable: 0})

This will update the variable and set it to 0.

Unless you have created the variable you will not be able to reference it anywhere.

 

To use the variable imply set the value of the blue box to MyVariable. As you type the name of your variable the full variable name will be suggested. If this doesn’t happen then you know that your variable hasn’t been initialized yet.

 

If your variable doesn’t exist yet. Run your app and click the Reset button. 

To verify if your variable has been created and which type of variable was created you can check your variables in  PowerApps Studio.

This is where you can also find the current value of the Variable.

To make your apps in PowerApps document themselves it might be wise to consider proper naming of your variable.

This is where I often prefix my number values with ‘num’ So numMyVariable would be my preferred option.

Then rather than using MyVariable it might make sense to use a more descriptive name for your variables.

Then finally it is important that you keep the type of variable the same throughout your app.

So for example if you created a number variable with the Set function like this Set(MyVariable: 0) and then later you used UpdateContext({MyVariable: “Some Text”}) then you might expect some issues within your app.

Then now it’s time to implement my Add 1 button. Within PowerApps it is not just possible to set a variable you can use all sorts of functions to set a variable. So in my case I simply want to add one to the existing value. This can simply be done my using the UpdateContext function again. UpdateContext({MyVariable: MyVariable + 1}) will do the trick.

If you need any further help or if you would like me to extend this post feel free to leave a comment below. Even if you would like me to help you develop your applications just contact me and I’m happy to help.

 

 

 

 

 

Advertisements