Code Snippet #3
For send a referral mail flow
This code snippet performs a series of operations involving data validation, database updates, and running a Power Automate flow to send an email. It uses conditional logic, collection filtering, data patching, and context variable updates.
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
)
);
Explanation
-
Initializing a Context Variable:-
UpdateContext({varErrorCode: 0});
: This initializes a context variablevarErrorCode
with the value0
. Context variables in Power Apps are used to store data that can be accessed across different screens and controls within the same app. -
First Conditional Block - Checking and Updating ‘Referral Codes’:- This block checks if there are any active referral codes using
IsEmpty
and aFilter
on the ‘Referral Codes’ data source. If no active referral codes are found, it performs aPatch
operation to add a new referral code (fromlblReferralCode.Text
) to the ‘Referral Codes’ database. If an active referral code exists, it setsvarErrorCode
to2
. -
Second Conditional Block - Checking and Updating ‘Referrals’:- This block checks if the referral email (from
txtFriendEmail.Value
) already exists in the ‘Referrals’ data source. If it doesn’t, a new record is added to the ‘Referrals’ database with details from the form, andvarErrorCode
is set to1
. If the email exists,varErrorCode
is set to2
. -
Third Conditional Block - Setting Email Subject Based on ErrorCode:- Depending on the value of
varErrorCode
, this block updates another context variablevarEmailSubject
. IfvarErrorCode
is1
, indicating a new referral or code, the subject is set to “Referral Instructions”. Otherwise, it’s set to “Resending-Referral Instructions”. -
Fourth Conditional Block - Sending an Email via Power Automate:- This block runs a Power Automate flow named ‘Send-a-email-to-friend-with-referral-code’, passing the friend’s email, the email subject, the referral code, and user details as parameters. If the flow runs successfully, it displays a success notification and resets the email and name input fields. If it fails, an error notification is displayed.
In summary, this code snippet handles the process of checking for existing referral codes and referrals, updating databases accordingly, setting email subjects based on certain conditions, and finally sending an email through Power Automate. It employs a systematic approach to managing the referral process, ensuring data integrity and providing user feedback throughout the process.
Happy #low-code learning
Amit Puri, Advisor and Consultant, Strengthening Digital Experiences, Modernize Cloud Journey with AI-Driven Transformation!