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 👍

Published by arpitpowerguide

My name is Arpit Shrivastava, who is a Microsoft MVP in the Business Applications category. I am a Microsoft Dynamics 365 and Power Platform enthusiast person who is having a passion for researching and learning new things and acquiring immense knowledge. I am providing consistent help, support, and sharing my knowledge through various Social Media Channels along with my Personal Blog, Microsoft Community, conducting online training and attending various 365 Saturday Events worldwide and sharing the best Solutions to the readers helping them achieve their goals and objectives in Customer Relationship Space.

13 thoughts on “PowerApps – How to use Variables in Canvas App

  1. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! uk iptv

    Like

  2. Just like on Personal Computers, there are malicious Apps on Android that contain Spyware, Viruses or other blokada for pc mac types of Malware. This article will discuss ways to keep Malware off your Android device, and look at the advantages and disadvantages of running Android antivirus software.

    Like

  3. On the surface, all Kajabi vs clickfunnels might appear to be pretty similar – many of them supply the same types of features, information, even user interfaces. However, when you really start comparing different apps, you'll often notice limited functionality, speed, and quality of display on many apps currently being utilized by small and large businesses alike. Essentially, what's being described here is the difference between a mobile web app and a native app.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: