Deduplicate, Keep the Order
Your extension builds the CC line for order confirmations by gluing together addresses from several sources — the customer's contact card, the salesperson, a notification setup table. Nobody checks for overlaps, so the same address sneaks in two or three times, sometimes with different casing or stray spaces, and customers get triplicate emails. You are writing the cleaner every send now goes through.
Requirements
Create a codeunit named "Recipient Deduplicator" with one public procedure:
procedure Dedupe(Recipients: Text): TextRecipients is a list of email addresses separated by semicolons (;). Entries may carry leading or trailing spaces, and the list may contain empty entries — nothing between two semicolons, or only spaces.
The returned text must follow these rules — all of them are graded:
- Trim leading and trailing spaces from every entry; entries that are empty after trimming are dropped.
- Two entries are duplicates when they are equal ignoring case:
Sales@Contoso.comandsales@contoso.comare the same recipient. - Keep only the first occurrence of each recipient, in the order recipients first appear, with that first occurrence's original casing.
- Join the kept entries with
'; '— a semicolon followed by exactly one space, with no leading or trailing separator. - If nothing survives — empty input, or only semicolons and spaces — return an empty text (
'').
What the tests check
The tests call Dedupe and compare the returned text exactly — note rule 4's semicolon-plus-space. Fixed cases cover a single recipient, exact duplicates, duplicates differing only in casing, entries with surrounding spaces, empty entries, an empty input, and an input of only separators and spaces. One test builds a randomized list where every address reappears later in upper case with padding, so hardcoding the fixed examples fails.