Tuesday 31 March 2020

PowerApps Collect Function - How to Improve your Canvas App Performance


Welcome back to everyone in the PowerGuide Mentorship Program. 

Today, I am gonna share a Tip - PowerGuideTip5 to improve Canvas App performance by using the Collect function.

Microsoft is already working hard to improve the performance of all apps that run on the Power Apps platform. But today, I am going to share one Tip that boosts the performance of apps that you create.


Let's get started...

What is Collect Function?


  • Collect is a kind of variable that is capable to store Collection of records/data in it.
  • Collect are sometimes used to hold global variables or make a temporary copy of a data source.
  • Collect can be used to store A Single Value, A Record and A Table.
  • Collect store the A Single Value, A Record and A Table in Canvas App local storage/cache.


When should I use Collect Function in my Canvas App?

There might be multiple scenarios where Collect Function might be helpful to use:

While Retrieving the Data from Data Source: Collect Function used to hold the temporary copy of your data source. By doing this, you can limit making the connection with the data source and also limit the number of queries you perform to retrieve the data from the data source.
This helps you to improve the Canvas App Performance.

While Pushing the Data to Data Source: Using Collect you can collect the data/record locally in your canvas app and then Push it into an actual database in one go, rather pushing it every time when you perform any operation from the Canvas App.


Can I clear the Collection after being created?

Collect values can also be cleared/removed depends upon the need. There are following ways to do that:

Clear - Clear function deletes all the records of a collection. The columns of the collection will remain as is.

Remove - Remove function is used to selectively remove the record from the Collections

RemoveIf- RemoveIf function to remove a record or records based on a condition or a set of conditions.

ClearCollect- ClearCollect function is the combination of Clear and Collect. It first Clears the Collection's data/records and then adds a new set of records to the same Collection


How can I read or retrieve all the Collection records?

ForAll - ForAll function used to read all the records available in a particular Table.


What is the Syntax of all these above functions?

Collect ( DataSource, Item, ... )

DataSource – Required. The data source that you want to add data to. If it does not already exist, a new collection is created.
Item(s) - Required. One or more records or tables to add to the data source.

Clear ( Collection )

Collection – Required. The collection that you want to clear.

Remove ( DataSource, Record1 [, Record2, ... ] [, All ] )

DataSource – Required. The data source that contains the record or records that you want to remove.
Record(s) – Required. The record or records to remove.
All – Optional. In a collection, the same record may appear more than once. You can add the All argument to remove all copies of the record.

RemoveIf ( DataSource, Condition [, ... ] )

DataSource – Required. The data source that contains the record or records that you want to remove.
Condition(s) – Required. A formula that evaluates to true for the record or records to remove. You can use column names from the DataSource in the formula. If you specify multiple conditions, all must evaluate to true for the record or records to be removed.

ClearCollect ( Collection, Item, ... )

Collection – Required. The collection that you want to clear and then add data to.
Item(s) - Required. One or more records or tables to add to the data source.

ForAll (Table, Formula)

Table - Required. Table to be acted upon.
Formula - Required. The formula to evaluate for all records of the Table.


Do we have some real-time examples to understand these all functions?

There are various real-time examples where we can use these functions.

Let me explain by taking an example of Field Service Agent, who uses a Mobile App (Canvas App) to handle his all sales/service activities and often visit at Customer Location based on his scheduled appointment.

During the customer visit, he might need to capture a lot of things while interacting with the customer.
Like:

  • Capture Notes/Discussions
  • Audio Recording of Meeting
  • Create multiple To-Do Tasks/Action Items
  • Assign the Task to the respective Sales Team in order to follow-up with the customer.
  • Click various Images/screenshots through Camera
  • Attach some supporting documents
  • Capture Customer Signatures and many more...

So while being in the meeting with the customer, a Field Service Agent has to do all these above activities.

Now, Just think about it, In order to perform these activities, every time I have to Push the data in the data source (Lets Suppose in our case it is Dynamics 365), then obviously it will impact the App performance. Because every time while performing the operation in Data Source (Dynamics 365), your app needs to make the Connection with D365, Perform the desired operations in D365 and return the result to the mobile app in order to display the actual result.

This is surely not the best solution from the architecture and user experience perspective.

In this type of requirement, the Collect function comes into the picture, where we can perform all these activities locally in your app and can hold the data/record in a temporary database. Once done with your work/appointment, you can push everything in the actual data source in one go.

Let's Implement this Canvas App and Learn how to each of these functions work in Real-Time:


Example of Collect:

Step 1: Create a Blank Canvas App with Blank Screen (ignore if you already have)  Connect it with Dynamics 365 Data Source > Connect with Task Entity



Step 2: Design the Screen1 by adding few controls as shown below (or design as per your requirement). Also add Vertical List Gallery to display Created Task's List.







Step 3: Add one more Screen2 in order to Create Task.



Step 4: Add Collect Function on onSelect property of Create button







Step 5: Set the Collection as a Data Source to Task Gallery in Screen 1




Step 6: Run the App and See the Output












Example of ClearCollect:

Step 1 to 3 will remain the same.

Step 4: Add ClearCollect Function on onSelect property of Create button


Step 5: Run the App and See the Output









Example of Removeif:

Step 1: Add Delete Icon in Your Task List Gallery


Step 2: Add RemoveIf function (as below) in order to delete the record base on the condition given.



Step 3: Run the App and See the Output



Example of Remove:


Step 1: Add Delete Icon in Your Task List Gallery

Step 2: Add Remove function (as below) in order to delete the selected record



Step 3: Run the App and See the Output





Example of ForAll:

Step 1: Add ForAll function to read Collection (as below) in order to Patch all the records in Dynamics 365


Step 2: Run the App and See the Output








Where can I see my Collection in PowerApps?

Here is the way to see your all Collections in Tabular Format


What are the other options in Canvas Apps in order to get the refreshed/updated data from the actual data source?

In Canvas App, once you create the data in the data source (D365), you have four possible options in order to get the updated data from the database.


  • Go to View > Data Sources > Right Click on the Entity/Table > Hit Refresh

  • Manually Refresh by adding Refresh(<Entity Name>); function on either click of some custom button/icon.



  • Automatically Refresh the data by adding Refresh(<Entity Name>); function on onVisible property of your Canvas App Screen





  • What is the validity of the data being stored in the PowerApps Collection?

    Data will be available in your PowerApps Collection unless you close the App. If you don't want to loose the data and want to use it further when you open your app again....we have some more interesting features that help PowerApps to work in Offline mode as well. I'll share it in my next post.



    I hope you liked this Tip and will use in our implementation to improve your App performance.

    In my next Tip- PowerGuideTip6, I will share one more interesting feature related to the Collect function through which you can work with PowerApp in Offline mode as well,

    Cheers and Stay Tuned 👍

    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 👍

    Tuesday 24 March 2020

    PowerApps - Design Popup/Dialog to display Error/Warning Message


    One of the most common requirement in any application/project is to Show Error/Warning Messages.

    Currently In PowerApps, there is no direct way to show popup/dialog box in order to show any information/warning/error message to end users.

    Today, as part of "PowerGuide Mentorship Program", I am going to give you PowerGuide - Tip3 where we'll learn, how to design Popup/Dialog Box in Canvas Apps

    Let's get started...

    Step 1: Create a Blank Canvas App > Connect with Dynamics 365 Data Source > Connect with Lead Entity

    Step 2: Add few Text Inputs and Button Control to design the Screen for Lead Creation


    Step 3: Add few Controls like Labels and Icons in order to design Popup/Dialog Box.



    Step 4: Declare Variable and Manage the Hide/Show of Popup/Dialog

    Let's understand the Logic to Show/Hide Popup/Dialog in 3 Steps:

    1. Set Popup/Dialog Visibility to FALSE On Load of Screen.
    2. OnClick of Submit Button, Check if lead of entered emailaddress already exist in D365, if Yes, then Set Popup/Dialog Visibility to TRUE else FALSE.
    3. OnClick of Close icon (X) inside dialog box, Set Popup/Dialog Visibility to FALSE.
    Note : Here Popup/Dialog is the Collection of various Controls that we are using to design the Popup/Dialog. That means, hiding the Popup/Dialog means you'll have to set all the controls Visibility to FALSE that has been used to design the Popup/Dialog and Showing the Popup/Dialog means you'll have to set the same controls Visibility to TRUE.

    Keep all the Popup/Dialog Box Controls Visibility to False on Load of Screen

    If Lead of same emailaddress already exist, then Set Popup/Dialog Box Visibility to True else False

    Set Variable to Popup/Dialog Box Control's Visible Property. In above screen, I have set Variable to only errorMsg label (as shown in highlighted screen). However, you have to do the same for all the Controls that you are using to design the Popup/Dialog Box


    Step 5: Set Visibility of Popup/Dialog Box Controls on Button Click.

    Hide All Popup/Dialog Box's Controls on (onSelect) property of Close Icon

    Step 6 - Run the App





    Hope this Tip helps someone to incorporate various requirements.

    Stay Tuned for my next Tip - PowerGuide - Tip4 as part of  PowerGuide Mentorship Program Series

    Cheers 👍

    Blogger Widgets