Editor’s Note: Kerri’s application stands out because of the way it cleverly integrates several extensions to provide a slick send email solution that has template-like features but allows the user to edit the content before sending. Check it out!
I had a real request come in where someone wanted to be able to send branded emails that could be edited and have files attached before sending. At first, this seemed like it might be a bit much, but I thought about it and went to Unofficial SF and installed some packages. I was happy to be able to complete this request without having to get into any code!
In this example, we will be using a Flow to send a rich text email from a button on the Account Page. We’ll be able to edit the email before it is sent and include attachments.
Button Example
Clicking the Send Award Email button triggers the Award Email Flow screen flow:
The flow opens a screen with a Rich Text email component, allowing for edits and formatting and also a file upload component:
The only feature in the edit screen that I noticed issues with is the image button. Any uploaded images in this manner are not included in the email:
Clicking Upload Files allows you to attach multiple files to the email and Project record:
The attachments and any changes to the Rich Text template are included in the email.
The Sent Email:
The related record:
Notes & Attachments
Files
Flow: Send Award Email
This Screen Flow allows the User to edit a Rich Text email template that includes a base image and allows for adding multiple attachments.
Prerequisite Packages:
- Rich Text Input – Flow Screen Component (richTextAreaFSC)
- Upgraded FLOW ACTION AND SCREEN COMPONENT BASEPACKS (FlowScreenComponentsBasePack)
- Send Richer Email with the ‘Send Better Email’ Flow Action (SendBetterEmail – 2.2.1 Unlocked)
Get and Set Variables:
The first step gets the record to relate this email to for merge fields. In this example, we’re using the Account. We’ll pull in fields from the Account to the Text Template:
Next we start our Email Address collection with the testGroupEmail. For this example, we want to include a group inbox email address as part of our recipient collection. I am starting with adding the address variable manually and will explain why in a moment:
testGroupEmail Variable:
The next step is to populate the rest of our Email Collection. Normally I would set the email collection variable to add all the values at once, but the “Send Better Email” Apex action will keep any extra commas from adding any empty fields and fail during sending.
Example: emailAddresses might look like test@gmail.com ,,,,,,
To resolve this, we need to check if there are values in each field we want to pull an email address from and only add those fields to the collection instead of blank values:
Text Templates
EmailTemplateRT
This is the Rich Text Template where we’ll store some default text, formatting, images:
Note: Extra carriage returns in the rich text seem to appear as ? in the email, so delete any extra spaces in front of your text.
To add the image, I had to switch to Classic, upload the image to a public folder and check Externally Available Image.
Right click on the image (don’t open in a new tab) and copy the address into the Flow Text Template <img src=””> code in Plain Text and the image appears when switching back to Rich Text and when the email is sent:
Note: It doesn’t look like Text Templates work with Files or uploading images to the Rich Text template – at least not for the purposes of sending an email:
EmailTemplateUpdated
This is the template that we will actually use in the “Send Better Email” Apex action. We’ll paste the variable from the Edit Email Screen into the body:
Rich Text side:
Plain Text side. Remove any html tags:
EmailTemplatePT
I’m not currently using this, but it’s just a Plain Text version of the template that can be included in the “Send Better Email” Apex action:
Email Preview Screen
Now, we set up our email edit screen. Drag the inputRichTextFSC component:
Rich Text Value is where we’ll reference our Text Template
I added a note to the Screen about not using the Image icon on the Rich Text Editor to add images because they won’t be included when the email is sent. I don’t see any way to hide this, so I wanted to provide a warning:
File Upload
To upload files to the email, drag the File Upload component to the screen:
File Upload Settings:
- Related Record ID – The record where this file will be uploaded. In this case, I pulled the record Id from the initial Get Account element
- Allow Multiple Files – Set to True to attach multiple files
Setting the Attachments to Variables
contentDocumentLinksGet1
We need to get our files that were uploaded in the form of Content Document Links in order to include them in the rich text email using SendBetterEmail. A caveat for this is that in order to use this functionality, the Content Document Link query must be filtered like:
- By ContentDocumentId OR LinkedEntityId using the Equals operator or multiple Id’s using the IN operator.
- You cannot use the Contains operator or any other in this filter.
Failing to filter correctly, will result in the following error when you try to send the email:
“This error occurred when the flow tried to look up records: Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id’s using the IN operator.”
How to filter using the IN Operator?
Set a Loop element to pull in the collection from contentDocumentLinksGet1. We only want to attach the Files that are uploaded during this Screen Flow to the email, not every File attached to the record, so we have to do some filtering.
Decision: Is Content Document Link part of the File Upload?
The File Upload record is not the same as a Content Document Link, but it does collect the Content Document and Content Document Links. When we attach a file or multiple files, File Upload sets a few variables:
- contentDocIds – Id to the Content Document of the File(s)
- contentVersionIds – sets the version of the File(s)
- recordId – the record where the File will live. It’s a Project record in this case.
Querying for the Content Document Link gets a little weird because it’s not set as a variable in the File Upload.
- Check if the Content Document Ids from the File Upload variable match the Content Document Id in our current Content Document Link in the loop.
- Conditions: {!AttachFile.contentDocIds} CONTAINS {!Loop_through_Content_Document_Links.ContentDocumentId}
If this is true, then we want this record.
contentDocumentLinksGet2
Under normal circumstances, DON’T DO THIS. It’s a bad practice to add DML statements and/or queries within Loops as it’s likely to cause the Flow to fail due to governor limits being reached (CPU errors, max SOQL queries reached, etc). In this case, we’re only looping through a few records that we’ll be attaching to an email. It’s also the only way I can think to get the Content Document Link records from the File Upload.
Again, we need to follow the specific filtering for getting Content Document Links:
Add the current Content Document Link to the record collection that we’ll use in our “Send Better Email” Apex action.
Note: Per discussion on UnofficialSF page for the SendBetterEmail component, this collection variable MUST be named contentDocumentLinks:
“Send Better Email” Apex action
After the last loop, we send the email. The “Send Better Email” Apex action is effective, but very sensitive to the variables you give it.
You can send one email (Standard) or Mass Email. In this example, we just want to send one email to multiple recipients.
Add the Recipients to the Set Addresses section. For Search Existing Flow resources, select String Collection and select our emailAddresses collection variable:
Subject and Body
- Set the Email Subject. This can be from a Text Template, Variable, etc or just typed in.
- Set the Email Body. Check Specify Body here since we’re using a Text Template and not an actual Email Template. Set HTML Body to the EmailTemplateUpdated Text Template.
Recipients & Recipient-Related Records
- Related Record Id: The record we’re pulling fields from and uploading the file to
Attachments
- Set to our contentDocumentLinks collection variable
- Q: Why do we have a template within a template?
- A: Sending using the edited template directly causes weird artifacts to appear in the email: