PowerApps – How to use Variables in Canvas App

As we all know that Variables are used to store the information to be referenced and manipulated in your application. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves.

It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your application.

Let’s understand the Variables in PowerApps.

Power Apps has three types of Variables:

Now Let’s understand each Variable type by taking an example from Dynamics 365 Perspective.

Example of Context Variable:

Create Parent Record in Dynamics 365 and map it in it’s Child Records.

Let suppose, I have to create a Contact in D365, however, in order to do that first I need to have an Account record created in D365.
Hence, I would be required to create Account (Parent Record) first, then only I’ll be able to create Contact (Child Record) so that I could refer the Account Record in the Contact record.

In this example, I have to use the Variable on the same screen only, so I can go with Context Variable (Local Variable)

Example of Global Variable: 

Show List of Records in Screen 1, and show it’s Details on Screen 2

Let suppose, I have a Canvas App > Screen 1 of List type where I am displaying a list of all Dynamics 365 Contacts. Now, If the user wants to view the detail of that Contact, he/she will click on a particular record and that will take user to the another Screen 2 where we need the GUID of that record in order to get its details.

In this example, I have to pass the variable value between Screen 1 and Screen 2, so I need to use a Global Variable.

Example of Collection Variable: 

Collections are sometimes used to hold global variables or make a temporary copy of a data source.

This type of variable can be used as a Cache in order to store the copy of your data source in your app locally so that you don’t need to perform query again and again. This helps to  improve the overall app performance,

In this PowerGuideTip4, I am going to share a Tip that, how we can use these variables in the Canvas App.


Implementation of Context Variable (Local Variable):

Step 1: Create a Blank Canvas App > Connect to Dynamics 365 Data Source or Common Data Service of your Environment > Connect with Account and Contact Entities.
Add few TextInput and Button Control in order to Create Account and Contact in D365 as shown below (or design as per your need)

Step 2: Add following Patch Formula on Button onSelect property and Navigate to Success Screen


Here AccountRecord is Local (Context) Variable, in which I am going to store Parent Record (Account) reference. I will pass this reference in Account Lookup (_parentcustomerid_value) while creating Contact record as shown below.


UpdateContext({AccountRecord: Blank()});

//Create a new Account record from the value entered in Company Field
UpdateContext({AccountRecord: Patch(Accounts, Defaults(Accounts), {name: company.Text })});

//Create a Contact with following data and Map above created Account in that
UpdateContext({ContactRecord:Patch(Contacts, Defaults(Contacts), {firstname: firstName_3.Text,lastname: lastName_3.Text,emailaddress1: emailAddress_3.Text,mobilephone: mobilephone_3.Text, _parentcustomerid_value: AccountRecord})});

// Navigate to Success Screen and Show newly created Account Name in Success Msg
Navigate(SuccessScreen,ScreenTransition.Fade);

Step 3: Test the App

Implementation of Set Variable (Global Variable):

Step 1 will remain the same.

Step 2: Add following Patch Formula on Button onSelect property and Navigate to Success Screen

Here, AccountName_GV is Global Variable, in which I am storing newly created Account Name, so that I can access it anywhere in my Canvas App

UpdateContext({AccountRecord: Blank()});

//Create a new Account record from the value entered in Company Field
UpdateContext({AccountRecord: Patch(Accounts, Defaults(Accounts), {name: company.Text })});

// Storing the newly created Account’s Name in Global variable ‘AccountName_GV’
Set(AccountName_GV,AccountRecord.’Account Name’); 

// Navigate to Success Screen and Show newly created Account Name in Success Msg
Navigate(SuccessScreen_GV,ScreenTransition.Fade);

Step 3: Create a new Success Screen (in my post it is: SuccessScreen_GV). Set the Global Variable that we have set in the previous step) value in your Success message

Step 4: Test the App

Implementation of Collect Variable:

Collect Variable is a broad topic. Therefore, I will share the use of Collect Variable in my next PowerGuideTip5. Stay Tuned.

Cheers đź‘Ť

- Advertisement -

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest News

How a robust Power Apps solution orchestrates thousands of project implementations

Managing moving parts is hard, and it’s costly to fumble big projects Struggles with monitoring project health can be perilous....
- Advertisement -

More Articles Like This

- Advertisement -