Code Snippet filter and updating a list of referrals

Chapter - 06

Visit to buy the book on Citizen Development https://go.citizendeveloper.codes/buynow

Code Snippet #7

Filter and updating a list of referrals

This code snippet demonstrates a combination of conditional logic, collection manipulation, and sorting. It’s designed to update and filter a collection named locReferralList based on certain conditions.

    UpdateContext({locFiltered: !locFiltered});
    If(
        locFiltered,
        ClearCollect(
            locReferralList,
            Filter(
                locReferralList,
                State in [
                    Icon.LogJournal,
                    Icon.Unlock
                ]
            )
        ),
        ClearCollect(
            locReferralList,
            {
                FriendName: "Steve",
                FriendEmail: "Steve@gmail.com",
                ReferralNotes: "The application submitted. The application is under review.",
                StateTip: "The application submitted",
                State: Icon.LogJournal,
                LastUpdated: DateAdd(
                    Now(),
                    -1
                )
            },
            {
                FriendName: "Vijay",
                FriendEmail: "Vijay@outlook.com",
                ReferralNotes: "The referral bonus unlocked! You will receive instructions for claiming the payout.",
                StateTip: "The referral bonus unlocked!",
                State: Icon.Unlock,
                LastUpdated: Now()
            },
            {
                FriendName: "Priyanka",
                FriendEmail: "Priyanka@gmail.com",
                ReferralNotes: "The referral bonus settled. The payout amount is credited in your account XX162",
                StateTip: "The referral bonus settled.",
                State: Icon.Money,
                LastUpdated: DateAdd(
                    Now(),
                    -5
                )
            },
            {
                FriendName: "Farida",
                FriendEmail: "Farida@gmail.com",
                ReferralNotes: "The applicant needs support from the admission team. Help needed on the admission documentation.",
                StateTip: "Admission Team Support Needed.",
                State: Icon.Support,
                LastUpdated: DateAdd(
                    Now(),
                    -4
                )
            },
            {
                FriendName: "John",
                FriendEmail: "John@gmail.com",
                ReferralNotes: "The applicant needs support from the admission team. Help needed on the admission documentation.",
                StateTip: "Admission Team Support Needed.",
                State: Icon.Unlock,
                LastUpdated: DateAdd(
                    Now(),
                    -9
                )
            },
            {
                FriendName: "Sundar",
                FriendEmail: "Sundar@gmail.com",
                ReferralNotes: "The referral bonus settled. The payout amount is credited in your account XX162",
                StateTip: "The referral bonus settled.",
                State: Icon.Money,
                LastUpdated: DateAdd(
                    Now(),
                    -10
                )
            },
            {
                FriendName: "Allen",
                FriendEmail: "Allen@gmail.com",
                ReferralNotes: "The applicant needs support from the admission team. Help needed on the admission documentation.",
                StateTip: "Admission Team Support Needed.",
                State: Icon.LogJournal,
                LastUpdated: DateAdd(
                    Now(),
                    -7
                )
            }
        )
    );
    If(
        !IsBlank(drpColumns.Selected.ColumnName),
        ClearCollect(
            locReferralList,
            SortByColumns(
                locReferralList,
                drpColumns.Selected.ColumnName,
                If(
                    locSortOrder,
                    Ascending,
                    Descending
                )
            )
        );
        
    );

Explanation

  • Toggle Filter State:- UpdateContext({locFiltered: !locFiltered});: This line toggles the value of a context variable named locFiltered. If locFiltered is true, it becomes false, and vice versa. This variable is used to track whether the list is filtered or not.

  • Conditional Collection Update:- The first If statement checks the state of locFiltered:
    • If locFiltered is true, it applies a filter to locReferralList and clears and re-collects it with only the items where the State is either Icon.LogJournal or Icon.Unlock.
    • If locFiltered is false, it clears and re-collects locReferralList with a predefined set of records, each containing details like FriendName, FriendEmail, ReferralNotes, StateTip, State, and LastUpdated.
  • Conditional Sorting of the Collection:- The second If statement checks if the selected item from a dropdown (drpColumns.Selected.ColumnName) is not blank. This dropdown allows the user to choose a column to sort by.
    • If a column is selected, it sorts locReferralList by the selected column. The direction of the sort (ascending or descending) is determined by the value of another context variable locSortOrder.
    • SortByColumns(locReferralList, drpColumns.Selected.ColumnName, If(locSortOrder, Ascending, Descending)): This function sorts locReferralList based on the selected column name and the sort order.

In summary, this code manages the data in the locReferralList collection based on user interactions. It allows toggling between a filtered and unfiltered state of the list, and it also provides functionality for sorting the list based on a user-selected column. The list can be filtered to show only certain types of referrals (based on the State field) and sorted by any column chosen by the user. This kind of dynamic data manipulation is common in Power Apps for creating interactive and responsive user interfaces.

Happy #low-code learning

Visit: www.citizendeveloper.codes

Id: Chapter-06-CS00007

Category: Chapter 06

Dr. Amit Puri, Advisor and Consultant, Strengthening Digital Experiences, Modernize Cloud Journey with AI-Driven Transformation!

Code Snippet # 4
Chapter-08-CS00004 - Code Snippet # 4
Code Snippet # 6
Chapter-08-CS00006 - Code Snippet # 6
Code Snippet # 5
Chapter-06-CS00005 - Code Snippet # 5