Code Snippet #6
For toggle a filter functionality
This code snippet is designed to toggle a filter on a collection of referral data based on a context variable (locFiltered). It updates another context variable (tabReferrals) with a filtered subset of the Referrals data source, based on the value of locFiltered.
UpdateContext({locFiltered: !locFiltered});
If(
locFiltered,
UpdateContext(
{
tabReferrals: Filter(
Referrals,
'Referrals (Views)'.'My Referrals',
ThisRecord.'Referral Status' = 'Referral Status (Referrals)'.'Application submitted'
)
}
),
UpdateContext(
{
tabReferrals: Filter(
Referrals,
'Referrals (Views)'.'My Referrals'
)
}
);
);
Explanation
-
Toggling the Filter State:-
UpdateContext({locFiltered: !locFiltered});: This line toggles the value of the context variablelocFiltered. IflocFilteredis currentlytrue, it becomesfalse, and vice versa. This variable is likely used to track whether a specific filter is applied to the referral data. - Conditional Filtering of Referrals:- The
Ifstatement checks the state oflocFiltered:- If
locFilteredistrue, it applies a specific filter to theReferralsdata source:UpdateContext({ tabReferrals: Filter(Referrals, 'Referrals (Views)'.'My Referrals', ThisRecord.'Referral Status' = 'Referral Status (Referrals)'.'Application submitted') }): This updates thetabReferralscontext variable with referral records where the status is ‘Application submitted’. It assumes that ‘Referrals (Views)’.’My Referrals’ is a predefined view or condition within theReferralsdata source.
- If
locFilteredisfalse, it applies a more general filter:UpdateContext({ tabReferrals: Filter(Referrals, 'Referrals (Views)'.'My Referrals') }): This updatestabReferralswith all referral records that meet the ‘My Referrals’ condition or view, regardless of their referral status.
- If
- Usage of UpdateContext: This
UpdateContextfunction is used to create or update a context variable within the current screen of a Power Apps application. Context variables are temporary and local to the screen they are set in.
In summary, this code snippet is used to toggle between two states of a filtered list of referrals: one where only referrals with the status ‘Application submitted’ are shown, and another where all referrals under ‘My Referrals’ are shown. This functionality is useful in scenarios where users need to switch views or filters in a list, such as in dashboards or data management interfaces.
Happy #low-code learning
Amit Puri, Advisor and Consultant, Strengthening Digital Experiences, Modernize Cloud Journey with AI-Driven Transformation!