Avoiding the 5 recipient limit on email action
A Warning in Advance
Skipping limits is dumb, you probably shouldn't do it, but history is filled with people doing dumb stuff and getting away with it. What's the worst that could happen here? Maybe an email doesn't get sent, maybe your release manager vomits at the sight of your commit?
Problem Statement:
I want to send an email for notification from a flow, but I want to send it to more than 5 people from the standard email component.
In this example, I'm going to set up a scheduled flow to check for expiring package licenses and notify the admin team, via the apex exception queue. (FYI - the apex exception email recipients list can be set as the default notification for flow debug emails, which is quite useful in smaller orgs)
Gather the email addresses into a collection:
Our first action is to collect the email addresses from the Apex Email Notification table:
And add the records to a text collection variable:
In this case, there are two possible locations for the email, as the notification emails can either hold an email address, or a lookup to a user, both can be added to the string collection.
Iterating our collection:
Our first action, once we have built our recipient collection, is to assign the count of records to a number variable. Then we can have a decision element to determine if we need to select only 5 recipients from the collection, or have fewer than five and can simply send the email.
you can see the decision above, with the looping process handled by the more than 5 branch.
How to iterate:
The process for iterating here is simple step-wise, but not overly obvious when starting from a blank canvas. If we were doing this in the natural way (how you might describe it without platform limitations, beyond the 5 recipient limit)
-
- Take the first 5 out of the recipient collection
- Send them the email
- Back to the collection count element and update
Step 1, unfortunately, is not available in salesforce, we instead will have to take a few more steps:
-
- Duplicate your collection into a temporary collection
- Sort your collection and limit to 5 using the Collection Sort element*
- Your temp collection now only has 5 recipients in it, so send them the email
- Remove those 5 recipients from your Original Collection
- Re-count the original collection and proceed to the decision
*Unlike the filter element, collection sort DOES modify the supplied collection, and DOES NOT accept resources as the limit variable. The modification is why we need to create a temporary collection for the 5 recipients in the loop.
Step 1:
Duplicate your collection into a temporary collection:
Last5Recipients is an identially configured variable text collection that we will use to hold our current batch. Using the equals means that any current values in the collection should be overwritten each time.
Step 2:
Sort your collection and limit to 5 using the Collection Sort element:
This selects us the last 5 recipients in the collection, these will be the target for the next email action
Step 3:
Your temp collection now only has 5 recipients in it, so send them the email
Step 4:
Remove those 5 recipients from your Original Collection:
The remove all operator for the assignment element removes all occurences of the Value from the Variable in our case, we are then removing the 5 recipients that we have just emailed, from the original recipient collection.
Step 5:
Re-count the original collection and proceed to the decision:
The connect to element option allows you to connect to other parts of your flow, simply select your Assign Count of Recipients element (see here) and you've created your loop.
One final warning: I have NO IDEA how far you could push this, I certainly wouldnt recommend building a huge notifiation list into this flow, there are better tools for mass emailing, and you're almost certian to have issues if you push this really far.
If you were feeling particularly cunning, you could package this into an autolaunched flow, receiving a collection of recipients, a subject, and body, then call it asynchronously from across your org, saving you from having to rewrite it every time.
No Comments