For  send a referral mail flow

Chapter - 08

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

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 variable varErrorCode with the value 0. 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 a Filter on the ‘Referral Codes’ data source. If no active referral codes are found, it performs a Patch operation to add a new referral code (from lblReferralCode.Text) to the ‘Referral Codes’ database. If an active referral code exists, it sets varErrorCode to 2.

  • 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, and varErrorCode is set to 1. If the email exists, varErrorCode is set to 2.

  • Third Conditional Block - Setting Email Subject Based on ErrorCode:- Depending on the value of varErrorCode, this block updates another context variable varEmailSubject. If varErrorCode is 1, 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

Visit: www.citizendeveloper.codes

Id: Chapter-08-CS00003

Category: Chapter 08

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

Code Snippet # 1
Chapter-08-CS00001 - Code Snippet # 1
Code Snippet # 9
Chapter-06-CS00009 - Code Snippet # 9
Code Snippet # 7
Chapter-08-CS00007 - Code Snippet # 7