Latest Posts

Power Apps - UpdateContext(), Set(), Navigate() functions for variables


In this article, we will see how we can set the variables in Power apps using UpdateContext(), Set(), and Navigate() functions. We will see the exact purpose of all of them as to when to use which one. We will also go into in-depth functionalities of all the functions.

Note:

  • UpdateContext – As the name suggests, this is used to set the context specific variables. This will keep the values only on temporary
  • UpdateContext variables scope is limited to the screen where it originated. That means you cannot use a context variable in other screen’s formula.
  • These variables are created using UpdateContext() or Navigate() function. We will see examples later in the article.
  • When your app is in edit mode, you can see the variables and their values under – File >> Variables.
  • Variables set using UpdateContext() can hold the values on the same screen.
  • Set() function is used to set the variable globally.
  • Once we use the Set() function, the variable is available throughout the app (until we close the app).
  • Both global (Set()) and local variables (UpdateContext() and Navigate()) values exist till the app is running. The moment you close the app, the variable is gone.

PowerApps UpdateContext() function Syntax:

UpdateContext({VariableKey:"VariableValue"})

  • VariableKey: This is Required. This is a record that specifies new values for some or all the context variables in the current screen.
  • VariableValue: This is Required. This is the value of the key defined. You need to give the VariableKey in the app and it will display the VariableValue. More in the Examples section.

PowerApps Set() function Syntax:

Set(VariableKey, "VariableValue")

  • VariableKey: This is Required. This is a record that specifies new values for some or all the context variables in the current screen.
  • VariableValue: This is Required. This is the value of the key defined. You need to give the VariableKey in the app and it will display the VariableValue. More in the Examples section.

UpdateContext() function examples

Set context variable and use it in PowerApps

  • In this example, we will place a button on the screen.
  • On the click event of the button, write the below command.UpdateContext({FirstName:"Nick"});
  • Power Apps UpdateContext
  • Here, FirstName is the key and Nick is the value.
  • Let’s use this variable.
  • Place a label on the screen. In the text property of the label, you can use the above variable just by writing the key, i.e. FirstName and the value (Nick) will be visible to the end user.
Power Apps Set

Set context variable using Navigate() function

  • Here, we will set and use the variable using Navigate() function.
  • On the click of a button place the function as mentioned below.Navigate(SecondScreen,ScreenTransition.Cover,{FirstName:"John"});
    • First parameter: This is the screen where you want to navigate to.
    • Second parameter: This is the transition animation when the user is navigated from one screen to another.
    • Third parameter: This is the parameter where we set our context variable. FirstName is the key and John is the value.
    • Power Apps Navigate
  • You can use this variable in the next screen, the same way we used it in the first example.
  • You just need to type the Key in the label text. i.e. FirstName

Some more Example on UpdateContext() function

  1. Multiple Key value pairs:
    • UpdateContext({FirstName:”Ian”, LastName:”Cooper”})
    • How to Use: you just need to type the Key like Firstname, LastName. That will print the respective value.

      Input: FirstName & " " & LastName.
      Output: Ian Cooper.
  2. UpdateContext({Student1:{FirstName:"Ian", LastName:"Cooper"}})
    • How to Use: Student1.Firstname, Student1.LastName
  3. Set(FirstName,”Ian”)
    • How to Use: Firstname

We value your Feedback:

Page URL:

Name:

Email:


Suggestion:

© 2023 Code SharePoint