These are examples to illustrate techniques that facilitate using the Send Better Email Action. They include:

Return to Send Better Email

Example: Create a Resource Variable Collection of Strings and Add Individual Email Addresses

Add a collection variable
Populate the collection variable
// for a string
String exampleSingleEmailAddress = "john.smith@example.com";
// or for collections
List<String> exampleStringCollection = new List<String>{"john.smith@example.com","jane.doe@example.com"};
List<Contact> exampleContactCollection = [SELECT Id Contact Limit 2];

Example: configure inputs with sources of email addresses:

The sendBetterEmail package ships with two screen flows that can be used to test and demonstrate the action, ‘Send Better Email Testflow’ and ‘sendBetterEmail Test – Create Test Templates if Needed’. You do not have to activate these flows to use them. Open ‘Send Better Email Testflow’ then select “Debug” (or “Run”).

Flow Examples in Send Better Email Testflow (Send_Better_Email_Testflow)

Flow Examples illustrate different ways of using this Action while providing the ability to test this action in your org. The test flow Send Better Email Testflow(Send_Better_Email_Testflow) not only provides a framework for testing in your org, but also shows some of the common usage patterns including:

  1. Flow resources setup and types of resources used in the Action
  2. Flow process paths that illustrate how setup prior to the action may occur
  3. The Action and parameters in different usage situations

Some of these examples may require prior setup of records. Where necessary, this is described in each of the selected examples. Examples/Tests include:

  1. Simple Email Text – with To, Sender, Reply-To, Subject, Body (All text)
  2. Simple Email with Address Collections – Using Address Collections to Send to Multiple Recipients
  3. Email with Attachments – Simple Email (1) With Attachments
  4. Email Using Template – An email using a Lightning EmailTemplate
  5. Multi-Lingual Using Email Templates – An email using a Lightning EmailTemplate selected by language
  6. Mass Email – Send an email using MassEmail messaging
  7. Bulk Email – Send bulk email using request collections

Choose the example you would like to see and the instructions are in each of the examples. You will also find in these examples the use of Text Templates for formatted Rich Text messages (Example 1), using Plain Text (Example 2), and logging the email as an activity on the related object (Example 5).

Select Test Flow and Start an example

To see how the Send Better Email Action is configured for each of these examples, click on the appropriate Action (they are labeled) and look at the Input Variables. You may also want to look at some of the logic in the setup to those examples for ideas in creating your own flows.

Flow Example 1 – Sends a simple better text email

  • content predefined in the resource HTMLTextTemplate
  • to the addressee in [TO(send_Email_To)]
  • from[Reply to Email(TestReturnAddress)]
  • with the name [Sender Display Name(testSenderName)]
  • using [Subject(testSubject)]
  • and [Body(x1_HTML_Body)]

All of the above are flow resource variables. To create a body with line breaks, use a formula variable (see example resource variable testPlainTextLineFeeds)Note: To get the full effect of rich text, edit the flow resource formula HTMLTextTemplate resource instead of modifying the field on this screen

Flow Example 2 – Send an email using collections of recipients

The collections are defined by:

  1. To: A collection of strings
  2. CC: A collection of Contact Ids derived from pre-existing Contacts selected via a “contains” configuration
  3. BCC: A collection of User Ids selected via a “contains” configuration

If you want to test the Contact Collection, you must specify pre-created contacts using “contains” which looks for contact names containing the specified string.

If you want to test the User Collection, you must specify existing users using “contains” which looks for user names containing the specified string.

In the flow, the following are constructed and used for the action:

  • TO (stringCollectionEmailAddresses) [from ToEmailAddress_2_1 & ToEmailAddress_2_2]
  • CC (curContacts) [from Test_Contact_Recipient_Contains_2]
  • BCC (curUsers) [from Test_User_Recipient_Contains_2]
  • from[Reply Email Address(testReturnAddress)]
  • with the name [Sender Display Name(testSenderName)]
  • using [Subject(testSendSubject)]
  • and [Body(x2_HTML_Body)]

All of the above are flow resource variables.

Flow Example 3 – Attachments and Signature

This example illustrates how to upload and send attachments using Content Document Links

Send the attachments added in the flow (related record == current user).  It also tests using the signature of the running user.  Parameters are:

  • to the addressee in [TO(send_Email_To)]
  • content predefined in the resource [HTMLTextTemplate]
  • from[Reply Email Address(TestReturnAddress)]
  • with the name [Sender Display Name(sendSender)]
  • using [Subject(sendSubject)]
  • and [Body(HTML_Body_3)]
  • with attachments [Attach which Content Document Links (filtered_cdls)](Created using document upload then converting documents to Document Versions and Content Document Links to those versions

All of the above are flow resource variables.

Flow Example 4 – Single Mail Template with merge fields from Recipient Record, Related To Record, Activity to Recipient and Task to Related To

NOTE: Before running this example flow, you must create record(s) for both the Recipient (a contact by default) and the Related Record (an Account Record by default) and have the Record Ids available.

This sends out an email using a specified Email Template with the addressee specified through the Recipient Record Id (TargetObjectId) and optionally (dependent on your Template) merge fields with an associated object record (WhatId).

Parameters used:

  • sender [Sender Display Name(sendSender)]
  • reply to [Reply Email Address(testReturnAddress)]
  • with  [Email Template Id(templateId)] (looked up or created if specified by name)
  • sent to [Recipient Record Id(templateTargetId)]
  • with related record [Related Record Id(templateWhatId)]
  • Save Email as Activity on Recipient Record(s)[true]
  • Save Email as Task on recipient related record(s)[true]

For this example, you will need to create or use a template. Templates also require that you have a recipient record (targetObjectId) (e.g., Contact, User, . . .) which must also pre-exist.

You will be required enter the ID for that record as the Recipient Record Id. Additionally if you intend to test merge fields (RelatedTo) with your template, You must also pre-create the related object record and have available the Id for that record as the Record Object Id (RecordId).

If you are using the defaults, you will also need to:

  1. Create a Contact (for Recipient Object) and/or have the Id available.
  2. Create an Account (for Related Record) and/or have the Id available.

This example also creates an activity for the email which will be assigned to the Recipient Record Id and a task related to the Related Record Id.

Note: These are independently settable in the Action.

If you specify template by name and the templates do not exist, they will be created using Contact for the Recipient RecordType and Account for the Related RecordType.

If you use the default test templates (sendBetterEmailTest) the Target ID should be a ContactId and the Related Record should be an AccountId.

Flow Example 5 – Sending Multi-Lingual with Email Templates

NOTE: Before running this example flow, you must create record(s) for both the Recipient (a contact by default) and the Related Record (an Account Record by default) and have the Record Ids available.

SendBetterEmail provides multi-lingual support within the same flow by allowing the language to be specified, then selecting the appropriate template for that language. For this functionality, each language localization has its own template with all localizations sharing a common EmailTemplate.Name. To differentiate the localizations, add a tag to the Email Template Description field in the format: ‘Language=”en_US”‘ (or whatever corresponding to your organization localizations)

This example sends out an email using an Email Template with multiple language versions selected by using the language identifier for a set of specified Email Templates with the language tagged in the description. It uses the Recipient Record Id (Recipient Id) and optionally (dependent on your Template) merge fields with a related object record (What/About Id) in merge fields to customize the email for each recipient/related object.

Parameters used:

  • description [Sender Display Name(sendSender)]
  • reply to [Reply Email Address(testReturnAddress)]
  • with [Template Name(testTemplateName)] (looked up or created if specified by name) (Default: sendBetterEmailTest)
  • selecting [Template Language(testTemplateLanguage)](Default: es_MX)
  • sent to [Recipient Record Id(templateTargetId)]
  • with related record [Related Record Id(templateWhatId)]

If you are using the defaults, prior to running this example, you must:

  1. Create a Contact (for Recipient Object) and/or have the Id available.
  2. Create an Account (for Related Record) and/or have the Id available.

If you have not already created the Named Template, this example will create an English and Spanish(Mexican) version of that template for you.

You can, of course, vary the templates and object records to reflect your organization’s needs.

This sends out an email using a specified EmailTemplate with the addressee specified through the TargetObjectId and optionally (dependent on your Template) merge fields with an associated object record using that record’s Id.

For this example, you will need to create or use one or more templates. Templates also require that you have a recipient record (targetObjectId) (e.g., Contact, User, . . .) which must also pre-exist. You will be required enter the ID for that record as the (targetObjectId).

Additionally if you intend to test merge fields (RelatedTo) with your template, you must also pre-create the related object record and have available the Id for that record as an ID for the Record Object Id (RecordId). If you use the default test templates (sendBetterEmailTest) the Target ID should be a ContactId and the Related Record should be an AccountId


Flow Example 6 – Send an email using MassEmail message

SendBetterEmail can also be used to send Mass Emails. Mass Emails are far more limited in functionality that Single Emails, though both can be bulked as a single send (up to 10) in this action. Mass emails cannot have language localization by target for obvious reasons (one template for a list of targets), so the selected language will apply to the entire list.

Since this is simply demonstrating the MassEmail functionality, only a single email is sent using the running user as the Recipient Record Id.

Parameters used:

  • description [Description(testMassEmailFinishedMessage)](sent in internal email with status after action completes)
  • with [Template Name(testTemplateName)] (looked up or created if specified by name) (Default: sendBetterEmailTest)
  • with related record id collection [Recipient Record Id Collection(UserListForMassMail)](consists of a single record, the user running the example)

You can, of course, vary the templates and object records to reflect your organization’s needs.


Flow Example 7 – Send bulk email using request collections

This is a complex, advanced functionality that should be used only when needed.  You are much better off using the standard functionality unless you have bulk requests that cannot be satisfied with the single SendBetterEmail request.

SendBetterEmail can be used to send bulk email using collections of; 1) Single Email Requests, 2) Mass Email Requests, or 3) a blend of both. All requests are (of course) subject to organization limits. While this example just shows how to create a collection of requests using the “Send Better Email Bulk Request Setup” Apex action where each request is a single email, each request in the collection could send as many as the request type allows (subject to limits of course). Consequently, if you create individual requests for the collection that each have a target object list (Recipient Records), each target will receive an email.

Parameters for sendBetterEmail Action (actually send the request collection):

  • Collection of sendBetterEmail Requests [emailRequests(bulkSendBetterEmailRequest)]

Parameters for ‘Send Better Email Bulk Request Setup’ (create a Single Email request)

  • to the addressee in [TO(send_Email_To)]
  • from[Reply to Email(TestReturnAddress)]
  • with the name [Sender Display Name(testSenderName)]
  • using [Subject(BulkSubject)] (formula)
  • and [Body(x1_HTML_Body)]

The flow works by creating a collection of requests, then send that collection to the sendBetterEmail action. There can be no more than 10 of each request type (Single, Mass) in a collection. The results of the collection request are returned as a collection of SendBetterEmailResponse, so if you want to check the results, you will need to loop through that response in the order of the original request collection.