For  generate or retrieve existing referral code

Chapter - 08

Visit to buy the book on Citizen Development https://go.amitpuri.com/buynow

Code Snippet #5

For generate or retrieve existing referral code

This code snippet is designed to generate or retrieve a referral code based on certain conditions. It employs a combination of conditional checks, data filtering, and string manipulation.

    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
    )

Explanation

  • Conditional Check Using IsEmpty and Filter:- IsEmpty(Filter('Referral Codes', 'Referral Codes (Views)'.'Active Referral Codes')): This part checks whether there are any active referral codes in the ‘Referral Codes’ data source. The IsEmpty function returns true if the filtered list is empty (i.e., there are no active referral codes).

  • Generating a New Referral Code:- If there are no active referral codes, the Concat function is used to generate a new referral code:
    • The Split function splits a long string of lowercase letters and numbers into individual characters.
    • ForAll iterates over each character, creating a table with each character as a separate record.
    • Shuffle randomly rearranges these records.
    • FirstN selects the first 8 characters from this shuffled list.
    • Concat then concatenates these characters to form a new 8-character referral code.
  • Retrieving an Existing Referral Code:- If there are active referral codes, the First(Filter(...)).Code part retrieves the code of the first active referral code found. It filters the ‘Referral Codes’ data source for active codes and returns the ‘Code’ field of the first record.

In summary, this code snippet checks for the existence of active referral codes. If none are found, it generates a new, random 8-character code. If active referral codes are available, it retrieves the first one. This logic can be useful in scenarios where unique referral codes need to be assigned, either by generating new ones or reusing existing active codes.

Happy #low-code learning

Visit: www.amitpuri.com

Id: Chapter-08-CS00005

Category: Chapter 08

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

Code Snippet # 7
Chapter-06-CS00007 - Code Snippet # 7
Code Snippet # 1
Chapter-06-CS00001 - Code Snippet # 1
Code Snippet # 8
Chapter-06-CS00008 - Code Snippet # 8