Wednesday 25 March 2020

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 👍

12 comments:

  1. It is truly a great and useful piece of information. how can i subscribe for a blog site? It is truly a great and useful piece of information.thanks for sharing these information
    c language interview questions

    ReplyDelete
  2. It is truly a great and useful piece of information. how can i subscribe for a blog site? It is truly a great and useful piece of information.thanks for sharing these information
    c language interview questions

    ReplyDelete
  3. It is truly a great and useful piece of information. how can i subscribe for a blog site? It is truly a great and useful piece of information.thanks for sharing these information
    c language interview questions

    ReplyDelete
  4. Thanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing... northshore connect patient login

    ReplyDelete
  5. I found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious... blackmart alpha android

    ReplyDelete
  6. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work... https://www.hookupscout.com

    ReplyDelete
  7. 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

    ReplyDelete
  8. Enjoy millions of the latest Android apps, games, music, movies, TV, books, magazines & more. Apps

    ReplyDelete
  9. URL Encoder. Enter text to URL encode or decode. Converts the text into a percent encoded string URL CODE

    ReplyDelete
  10. 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.

    ReplyDelete
  11. plastic canvas software free The love for plastic canvas cuts across all ages. Plastic canvas is a multi-faceted lightweight craft material with recurrently spaced holes.

    ReplyDelete
  12. 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.

    ReplyDelete

Blogger Widgets