Monday, September 27, 2021

PowerApps: Context Variable With Navigate Function

 Hello Friends,

Welcome back with another post on PowerApps. We all know that we can define two types of variables in PowerApps-

  1. Global Variable:- These types of variables are defined using Set function. Scope of these types of variables is entire App.
    1. Set(varGlobal001, "Global Variable 1")
      
  2. Context Variable:-  These types of variables are defined using UpdateContext function. Scope of these types of variables is the same screen. These variables are not available on other screens in that App.
    1. UpdateContext({varContextVariable:"Context Variable 1"})
      
But, if I say that you can access Context Variables on other screen to which you are navigating, then you will be surprised.
Yes, we can access the Context Variables on other screens as well. But there is a condition. Let me explain. We have Navigate function which is used to navigate from one screen to other. Everyone provides 2 values to this function-
  1. ScreenName (to which user has to be navigated)
  2. ScreenTransitionType (transition type of screen during navigation. i.e. None, Fade, Cover...)
Now, rare of you knows that the Navigate function has one more parameter called context. It is optional type parameter. You can pass a record that contains the name of at least one column and a value for each column. This record updates the context variables of the new screen as if passed to the UpdateContext function.
Let's understand with an example.
  1. Create a PowerApp. By default Screen1 will be added automatically. Now add one more blank screen "Screen2".
  2. Navigate to Screen1 and add 2 labels, 2 input text boxes and 1 button.
  3. Now add below Navigate function on Button >> OnSelect property.
  4. Navigate(Screen2,ScreenTransition.Fade,{varText1:TextInput2.Text,varText2:TextInput3.Text})
    

  5. Remember, till now, we haven't perform any activity on Screen2.
  6. Now, click on Screen2 and add 2 labels. Now change the Text property on these labels. The moment you start writing vart, it will populate both variables, we have defined on previous screen in Navigate function.
  7. Assign varText1 variable to first label and varText2 variable to second variable.
  8. You may add one button to navigate back to Screen1.
  9. Now save the app and play. Below are 2 screenshots. The first one is on Screen1 where I input "Hello" and "World" as input text to first and second textbox respectively.
  10. Now clicked on Navigate button. Second screen appears where first label is showing the value of first variable while second label is showing value of second variable.
  11. You may play with this by going back, changing the text box values and navigating again to Screen2.

  12. This way you can use Context Variables to pass information from one screen to other screen. Using this feature, you can remove dependency upon Global Variables, thus resulting the App performance.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.