Saturday, July 10, 2021

PowerApps: Create Repeater Control Part-1

 Hello Friends,

Welcome back with another post on PowerApps. Have you ever encountered a scenario where you have to create a Master-Child relationship list and you have to input data in both table simultaneously. Let me take an example. Suppose, you have to create an application for Expense Management. A user went for a leisure trip and he/she wants to keep information of each expense done for that trip. But the problem is that the trip is 6 days long so, he/she cannot remember all the expenses at the end of the trip. He/she want to have an app where he/she can create a master expense named link "Trip to Mumbai". Now for this trip, each expense can be saved as and when occurred. If we have to develop this functionality in any other language/platform, it can be each managed using JavaScript. In PowerApps, there is no control available nor any support of JavaScript. So, How can we achieve this? Let's try-

  1. First create two lists-
    1. Expense Master - It will store the master information of Expense
    2. Expense Details - It will store the detailed information of expenses that are linked to Expense master item.
  2. Below is the list creation-



  3. Now coming to PowerApps. 
  4. First we create a Welcome Page (ScreenWelcome) where we will add 2 buttons, one for New Expense another one for View Expense.


  5. The OnSelect property of "Add New Expense" will be described as follows-
  6. NewForm(FormNewExpenseMaster);
    ClearCollect(
        ExpenseDetailsCollection,
        {
            ExpenseItem: "",
            ExpenseCost: 0,
            ShowSaveButtons:true
        }
    );
    Navigate(
        ScreenNewExpense,
        ScreenTransition.CoverRight
    );
    Set(IsExpenseDetailsSaveFirst,false);
    
  7. Here we are creating a collection "ExpenseDetailsCollection" to hold the Expense Details so that when form is submitted, it will also be saved in transactional list. This collection has 3 fields-
    1. ExpenseItem:- It will hold the type/name of the Expense that was occurred.
    2. ExpenseCost:- It will hold the amount of the Expense.
    3. ShowSaveButtons:- it will tell the gallery that whether it has to show the Save icons or the Edit icons. It true, then show Save Icons otherwise Edit Icons.  
  8. A global variable "IsExpenseDetailsSaveFirst" is also defined. The purpose of this variable is to prompt the user that he/she has to save the transaction table changes (first) in local collection so that when form is submitted, all transactions will get saved in list.
  9. Another button "View / Edit Expenses" will have a simple redirection to View Screen when clicked.
  10. OnSelect >> Navigate(ScreenViewEditExpense,ScreenTransition.CoverRight); 
  11. Now we will create the New Expense Screen (FormNewExpenseMaster).


  12. The naming convention of controls is as below-


  13. Below is the controls added on above screen-


  14. Let start functional coding-
  15. IconExit
    1. OnSelect >> Exit(false);


  16. Label6
    1. Visible >> IsExpenseDetailsSaveFirst
    2. Text >> "You have unsaved Expense Details."


  17. btnSaveToSharePoint
    1. DisplayMode >> If(IsExpenseDetailsSaveFirst,Disabled,Edit)
    2. OnSelect >> SubmitForm(FormNewExpenseMaster)


  18. IconHome
    1. OnSelect >> Navigate(ScreenWelcome,ScreenTransition.Cover)


  19. IconNewExpenseDetailsEntryRow
    1. OnSelect >> Collect(ExpenseDetailsCollection,{ExpenseItem:"",ExpenseCost:0,ShowSaveButtons:true})
    2. Visible >> 
      1. If(
            CountRows(ExpenseDetailsCollection) > 0,
            If(
                CountRows( Filter(ExpenseDetailsCollection,ExpenseItem = "")) > 0,
                false,
                true
            ),
            true
        )
        


  20. FormNewExpenseMaster >>
    1. DataSource >> ExpenseMaster
    2. DefaultMode >> FormMode.New


  21. DataCardValue
    1. Default >> Choices([@ExpenseMaster].'Expense Type')


  22. DataCardValue2
    1. DefaultDate >> Today()


  23. DataCardValue3
    1. Default >> Text(Sum(ExpenseDetailsCollection,ExpenseCost),"#,###0.00")


  24. GalleryNewExpenseDetails
    1. Items >> ExpenseDetailsCollection
  25. IconTrashExpenseDetails
    1. Visible >> !ThisItem.ShowSaveButtons
    2. OnSelect >> Remove(ExpenseDetailsCollection,ThisItem)
  26. IconEditExpenseDetails
    1. Visible >> !ThisItem.ShowSaveButtons
    2. OnSelect >> Patch(ExpenseDetailsCollection,ThisItem,{ShowSaveButtons:true});UpdateContext({IsItemInEditMode:true});
  27. IconDeleteExpenseDetails
    1. Visible >> ThisItem.ShowSaveButtons
    2. OnSelect >>
      1. If(
            IsItemInEditMode,
            Patch(
                ExpenseDetailsCollection,
                ThisItem,
                {ShowSaveButtons: false}
            );UpdateContext({IsItemInEditMode:false});,
            Remove(
                ExpenseDetailsCollection,
                ThisItem
            )
        );
        
  28. IconSaveExpenseDetails
    1. Visible >> ThisItem.ShowSaveButtons
    2. OnSelect >> 
      1. If(
            !IsBlank(glytxtExpenseItem.Text),
            If(
                Value(glytxtExpenseCost.Text) > 0,
                Patch(
                    ExpenseDetailsCollection,
                    ThisItem,
                    {
                        ExpenseItem: glytxtExpenseItem.Text,
                        ExpenseCost: Value(glytxtExpenseCost.Text),
                        ShowSaveButtons: false
                    }
                );
                If(
                    !IsItemInEditMode,
                    Collect(
                        ExpenseDetailsCollection,
                        {
                            ExpenseItem: "",
                            ExpenseCost: 0,
                            ShowSaveButtons: true
                        }
                    )
                );
                UpdateContext({IsItemInEditMode: false});
                ,
                Notify(
                    "Please provide Expense Cost.",
                    NotificationType.Error
                )
            ),
            Notify(
                "Please provide Expense Item Name.",
                NotificationType.Error
            )
        );
        Set(IsExpenseDetailsSaveFirst,false);
        
  29. glytxtExpenseCost
    1. DisplayMode >> If(ThisItem.ShowSaveButtons, DisplayMode.Edit,DisplayMode.View)
  30. glytxtExpenseItem
    1. DisplayMode >> If(ThisItem.ShowSaveButtons, DisplayMode.Edit,DisplayMode.View)
  31. DataSource are-


  32. This way we have create the entry form for Expense manager. Here you will see that the gallery "GalleryNewExpenseDetails" is acting as Repeater Control. By default the input controls are shown in edit mode. As soon as you click on Save Icon, the row get inserted into local collection and at the same moment, the controls converts into View mode. The icons of Save / Cancel gets replaced by Edit / Trash. Additionally, new input row gets added into the gallery.
  33. Once all the information gets filled on the screen, you may click "Submit Expense" button to submit the data into SharePoint.
  34. Let's take an example-
  35. We have submitted an Expense for Business Meeting. In Expense Master, the ItemId for master record is 5.
  36. For this master record, the expense details got captured in Expense Details list with ExpenseMasterID as 5.
  37. If you sum up the expenses, it will come out to be 8850/- which is same as mentionend in ExpenseMaster list.
  38. ExpenseMaster - Records -


  39. ExpenseDetails - Records-


  40. With this Part 1 of this series is completed. In Part 2, we will try to edit the record, which is already inserted in system.

With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.