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 namedlocFiltered
. IflocFiltered
istrue
, it becomesfalse
, 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 oflocFiltered
:- If
locFiltered
istrue
, it applies a filter tolocReferralList
and clears and re-collects it with only the items where theState
is eitherIcon.LogJournal
orIcon.Unlock
. - If
locFiltered
isfalse
, it clears and re-collectslocReferralList
with a predefined set of records, each containing details likeFriendName
,FriendEmail
,ReferralNotes
,StateTip
,State
, andLastUpdated
.
- If
- 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 variablelocSortOrder
. SortByColumns(locReferralList, drpColumns.Selected.ColumnName, If(locSortOrder, Ascending, Descending))
: This function sortslocReferralList
based on the selected column name and the sort order.
- If a column is selected, it sorts
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
Amit Puri, Advisor and Consultant, Strengthening Digital Experiences, Modernize Cloud Journey with AI-Driven Transformation!