I recently ran into a behavior which I had not encountered before. There was a global variable in the app which is very common. But accessing that variable in a screen was resulting in a completely different value. After investigating it, I found that the screen had a context variable with the same name! This is what lead me to investigate the battle of global vs context variables in Power Apps!
Personally, I will not suggest global and local variables having the same name. If you do, then accessing the variable, in say a label to display its value, will result in the value of the context variable being displayed. So, if global and context variables have the same name, the context variable takes precedence. Use the disambiguation operator to access the global variable.
In the example below, I have a global variable declared in the OnVisible property of a screen. I then use a button to create a context variable, with the same name. To display the value of the context variable I need to access the variable with just the variable name. To display the value of the global variable I need to access the variable with the disambiguation operator.
Be careful if you have global and context variables with the same names!
PS: Read the official documentation here in Microsoft docs.
Have fun! Get addicted!
1 thought on “Global vs Context variables”