Flow: Input Rich Text to Email

The Situation

UnofficialSF hosts a bunch of useful components for use in Flows and elsewhere in Salesforce. One of the best is the Send Better Email component (from Jack Pond, Andrii Kraiev and Alex Edelstein) that allows for seamless and stylish HTML emails to be sent out of Flow (amongst other amazing features). While building out the Input Rich Text component also featured on the site, the email use case was not front of mind. Since that time, a few different users have brought up the natural interaction between these components and it is apparent that they may frequently be used together.

The Problem

Eilon Goldstein and Liad Fogel of Home Hero uncovered an interesting behavior in their application of passing rich text from Input Rich Text to Send Better Email. There is an interesting property of Rich Text fields in Salesforce lightning that results in an odd behavior when sending emails using the Rich Text field as the email body. Line breaks in Rich Text fields are saved as <p></p> in the HTML, and empty lines as <p><br></p> (documentation here). As a result, when using a Rich Text field filled in a Flow (or in the UI) to represent the body of an email, the output includes what amounts to extra line breaks. Below is an example of a two line entry in Rich Text:

<p>test 1 (from flow)</p><p>test 2 (from salesforce UI)</p>

The resultant email in such a scenario thus has extra spacing.

This image has an empty alt attribute; its file name is image-9.png

And of a two line entry with empty line between them:

<p>test 1 (from flow)</p><p><br></p><p>test 2 (from salesforce UI)</p>

This results in an especially spaced out email…

The Solution

To deal with this situation, we need to transform the Rich Text value to replace the inner <p></p> tags with a simple <br> and any <p><br></p> series with just <br> as well. This ensures that WYSIWYG from Flow field => email. While a bit clumsy, putting the input value through the following formula, and then mapping this formula to the email body, should do the trick. There are a few different formula approaches that will work here, and I’m not advertising this one as the best, but rather just the first one I did! I’m sure all you clever people can find many better approaches, but you get the idea. Replace {!InputText.value} with the specific Rich Text field you wish to transform of course

"<p>"+ TRIM(RIGHT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE({!InputText.value},"<p><br></p>","<br>"), "<p>", "<br>"),"</p>",""),LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE({!InputText.value},"<p><br></p>","<br>"), "<p>","<br>"),"</p>",""))-4))+"</p>"

The email output thus matches more accurately what I’ve entered and what I am expecting:

The Wrap-Up

This may be a small issue, but I expect it will be seen a lot as Input Rich Text is used with Send Better Email, and with custom email invocations from Flow as well (not limited to Flow either of course). Note that while this will work to transform at Run-time, any edit of the field from UI or Flow will reset any values you’ve saved to Database. I suppose you could save to Long Text Area with the tags whenever edit to Rich Text is made and then use that for the body of emails as an alternative solution.

Hope this has been helpful and assists to avoid some frustration in utilizing these tools!

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *