Code Snippets - Citizen Development in Microsoft 365, Empower Your Apps: Power Apps and Dataverse

Download the code snippets file

Chapter-08-Code-Snippet #1
// Code-Snippet #1

Switch(
    ThisItem.'Referral Status',
    'Referral Status (Referrals)'.'Referral bonus settled',
    Icon.Money,
    'Referral Status (Referrals)'.'Admission team support needed',
    Icon.Support,
    'Referral Status (Referrals)'.'Referral bonus unlocked',
    Icon.Unlock,
    'Referral Status (Referrals)'.'Referral code shared',
    Icon.Key,
    Icon.LogJournal
)

Chapter-08-Code-Snippet #2

// Code-Snippet #2

Switch(
    ThisItem.'Referral Status',
    'Referral Status (Referrals)'.'Referral bonus settled',
    "Referral bonus settled",
    'Referral Status (Referrals)'.'Admission team support needed',
    "Admission team support needed",
    'Referral Status (Referrals)'.'Referral bonus unlocked',
    "Referral bonus unlocked",
    'Referral Status (Referrals)'.'Referral code shared',
    "Referral code shared",
    "Application submitted"
)

Chapter-08-Code-Snippet #3

// Code-Snippet #3

UpdateContext({varErrorCode: 0});
If(
    IsEmpty(
        (Filter(
            'Referral Codes',
            'Referral Codes (Views)'.'Active Referral Codes'
        ))
    ),
    Patch(
        'Referral Codes',
        Defaults('Referral Codes'),
        {Code: lblReferralCode.Text}
    );
    UpdateContext({varErrorCode: 1});
    ,
    UpdateContext({varErrorCode: 2})
);
If(
    IsEmpty(
        Filter (
            Referrals,
            ThisRecord.'Referral Email' = txtFriendEmail.Value
        )
    ),
    Patch(
        Referrals,
        Defaults(Referrals),
        {
            Name: txtFriendName.Value,
            'Referral Email': txtFriendEmail.Value,
            'Referral Notes': "The Referral code shared is shared with a friend.",
            'Referral Status': 'Referral Status (Referrals)'.'Referral code shared'
        }
    );
    UpdateContext({varErrorCode: 1});
    ,
    UpdateContext({varErrorCode: 2});
    
);
If(
    varErrorCode = 1,
    UpdateContext({varEmailSubject: "Referral Instructions"}),
    UpdateContext({varEmailSubject: "Resending-Referral Instructions"});
    
);
If(
    'Send-a-email-to-friend-with-referral-code'.Run(
        txtFriendEmail.Value,
        varEmailSubject,
        lblReferralCode.Text,
        User().FullName,
        txtFriendName.Value
    ),
    Notify(
        "Email sent to " & txtFriendEmail.Value,
        NotificationType.Success
    );
    Reset(txtFriendEmail);
    Reset(txtFriendName);
    ,
    Notify(
        "Error, We are not able to send mail!",
        NotificationType.Error
    )
);

Chapter-08-Code-Snippet #4

// Code-Snippet #4

If(
    IsMatch(
        txtFriendEmail.Value,
        "^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$"
    ) && Last(
        ForAll(
            Split(
                txtFriendEmail.Value,
                "@"
            ),
            {Result: ThisRecord.Result}
        )
    ).Result in [
        "gmail.com",
        "outlook.com"
    ],
    DisplayMode.Edit,
    DisplayMode.Disabled
)

// or simply use, if you do not want to limit gmail.com or outlook.com
// If(IsMatch(txtFriendEmail.Text, Match.Email), DisplayMode.Edit,DisplayMode.Disabled)

Chapter-08-Code-Snippet #5

// Code-Snippet #5

If(
    IsEmpty(
        Filter(
            'Referral Codes',
            'Referral Codes (Views)'.'Active Referral Codes'
        )
    ),
    Concat(
        FirstN(
            Shuffle(
                ForAll(
                    Split(
                        "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789",
                        ""
                    ),
                    {Result: ThisRecord.Result}.Result
                )
            ),
            8
        ),
        ThisRecord.Value
    )    
    ,
    First(
        Filter(
            'Referral Codes',
            'Referral Codes (Views)'.'Active Referral Codes'
        )
    ).Code
)

Chapter-08-Code-Snippet #6

// Code-Snippet #6 icoFilter -> OnSelect

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'
            )
        }
    );
    
);


Chapter-08-Code-Snippet #7

// Code-Snippet #7 icoSort -> OnSelect

UpdateContext({locSortOrder: !locSortOrder});
UpdateContext(
    {
        tabReferrals: Filter(
            SortByColumns(
                Search(
                    Referrals,
                    txtSearch.Value,
                    "nledu_name",
                    "nledu_referralemail",
                    "nledu_referralnotes"
                ),
                "nledu_referralstatus",
                If(
                    locSortOrder,
                    SortOrder.Ascending,
                    SortOrder.Descending
                )
            ),
            'Referrals (Views)'.'My Referrals'
        )
    }
);

Chapter-08-Code-Snippet #8

// Code-Snippet #8 icoReload -> OnSelect

Reset(txtSearch);
UpdateContext(
    {
        locSortOrder: false,
        locFiltered: false
    }
);
Refresh(Referrals);
UpdateContext(
    {
        tabReferrals: Filter(
            Referrals,
            'Referrals (Views)'.'My Referrals'
        )
    }
);

Chapter-08-Code-Snippet #9

// Code-Snippet #9 icoSearch -> OnSelect

UpdateContext(
    {
        tabReferrals: Filter(
            SortByColumns(
                Search(
                    Referrals,
                    txtSearch.Value,
                    "nledu_name",
                    "nledu_referralemail",
                    "nledu_referralnotes"
                ),
                "nledu_referralstatus",
                If(
                    locSortOrder,
                    SortOrder.Ascending,
                    SortOrder.Descending
                )
            ),
            'Referrals (Views)'.'My Referrals'
        )
    }
);


Chapter-08-Code-Snippet-#10

// Code-Snippet-#10 Screen -> OnVisible

UpdateContext(
    {
        locSortOrder: false,
        locFiltered: false
    }
);

Reset(txtSearch);
UpdateContext(
    {
        locSortOrder: false,
        locFiltered: false
    }
);
Refresh(Referrals);
UpdateContext(
    {
        tabReferrals: Filter(
            Referrals,
            'Referrals (Views)'.'My Referrals'
        )
    }
);