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
IsEmptyandFilter:-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. TheIsEmptyfunction returnstrueif 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
Concatfunction is used to generate a new referral code:- The
Splitfunction splits a long string of lowercase letters and numbers into individual characters. ForAlliterates over each character, creating a table with each character as a separate record.Shufflerandomly rearranges these records.FirstNselects the first 8 characters from this shuffled list.Concatthen concatenates these characters to form a new 8-character referral code.
- The
- Retrieving an Existing Referral Code:- If there are active referral codes, the
First(Filter(...)).Codepart 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
Dr. Amit Puri, Advisor and Consultant, Strengthening Digital Experiences, Modernize Cloud Journey with AI-Driven Transformation!