function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Collapsing white space in plain text email templates


Hello...I consolidated 6 plain text email templates into 1 using conditional mark up (CASE) thus rendering the appropriate phrasing for the particular instance of the email. This worked like a charm and enabled me to have one CommandButton on a related VisualForce page instead of the 6 I had previously had.
Unfortunately the client made a follow up request: consolidate two of them further into one email but with much less content. On the surface it didn’t sound like too much work until I realized the only way to still use the same template was to obscure whole sections of text for this final scenario.
Below is a snippet of what I’ve been testing in the plain text template:

{!CASE(Session__c.Email_Notification__c, "FIRST APPOINTMENT", “START OF TEST”, “START OF TEST”)}
{!CASE(Session__c.Email_Notification__c, "FIRST APPOINTMENT", “”, “Please pay special attention to instructions below.”)}

{!CASE(Session__c.Email_Notification__c, "FIRST APPOINTMENT", “”, “Instructions:”)}
{!CASE(Session__c.Email_Notification__c, "FIRST APPOINTMENT", “”, “In lieu of a tire blow out, keep your ipad strapped in with a seat belt. Rear seating is always preferable for an ipad. But enough about Pat Metheny.”)}
{!CASE(Session__c.Email_Notification__c, "FIRST APPOINTMENT", “END OF TEST”, “END OF TEST”)}


The above code renders great for records that are not flagged “FIRST APPOINTMENT” e.g

Please pay special attention to instructions below.

In lieu of a tire blow out, keep your ipad strapped in with a seat belt. Rear seating is always preferable for an ipad. But enough about Pat Metheny.

But it literally puts white spaces for records that are flagged "FIRST APPOINTMENT", so they render as:

I am looking for a way to completely collapse white space as 75% of the template ends up rendering as white space for the FIRST APPOINTMENT scenario.
I also tested the following:
{!IF(ISPICKVAL(Session__c.Email_Notification__c , "FIRST APPOINTMENT"), "random1", NULL)}
{!IF(ISPICKVAL(Session__c.Email_Notification__c , "FIRST APPOINTMENT"), NULL, "random2")}
{!IF(ISPICKVAL(Session__c.Email_Notification__c , "FIRST APPOINTMENT"), "random3", NULL)}

The code above renders as:

random 3

Suggestions greatly appreciated!



I should add that this approach arose as a workaround to my initial desire to substitue whole sections of text that spanned multiple paragraphs and bullet points within the conditional statement. For example (and ideally):



{!CASE(session__c.email_notification__c,"FIRST APPOINTMENT", "You can go with this", 
"Or you can go with that.<br> This is the start of a new line and now for some bullet points:<br>
*Bullet point 1<br>
*Bullet point 2<br>
That's all folks")}

 This would hopefully have left fewer white spaces as I would not have needed so many conditional statements i.e. to render each paragraph.


If anyone has ideas on how to make this happen please share. Have tried BR() and \n to break up a string but can't get them to render as needed. 






It's working the way it's supposed to, the trick is in that you're inserting "invisible" control characters by putting each of your case statements on a new line. So if you were to drop your text into an editor capable of showing the hidden chars you'd see something line:
{!CASE(Session__c.Email_Notification__c, "FIRST APPOINTMENT", “START OF TEST”, “START OF TEST”)}\n
{!CASE(Session__c.Email_Notification__c, "FIRST APPOINTMENT", “”, “Please pay special attention to instructions below.”)}\n
... etc,
Note that the newline char isn't always shown as "\n" and sometimes "\r" is used instead but I'm sure you get the gist.
Now there are several potential fixes although the simplest is probably to put all of your case statements on one line ie. don't press the enter key when typing them in, and include the newline char in your case statement e.g.
{!CASE(Session__c.Email_Notification__c, "FIRST APPOINTMENT", “START OF TEST\n”, “START OF TEST\n”)}{!CASE(Session__c.Email_Notification__c, "FIRST APPOINTMENT", “”, “Please pay special attention to instructions below.\n”)}{!CASE(Session__c.Email_Notification__c, "FIRST APPOINTMENT", “”, “Instructions:\n”)}{!CASE(Session__c.Email_Notification__c, "FIRST APPOINTMENT", “”, “In lieu of a tire blow out, keep your ipad strapped in with a seat belt. Rear seating is always preferable for an ipad. But enough about Pat Metheny.\n”)}{!CASE(Session__c.Email_Notification__c, "FIRST APPOINTMENT", “END OF TEST\n”, “END OF TEST\n”)}
Make sense? Sorry about the formatting but this richtext input area is doing weird things when I try doing anything fancy.



Although what I've said above is technically true it seems that it's a bit tricky to introduce newlines into plain text email templates :/


I've had a look through the boards and tried a few things myself but it seems it's tricky one way or the other. This is what I tried with limited success:


<apex:outputText escape="false" value="{!CASE($CurrentPage.Parameters.wes,'FIRST APPOINTMENT', 'You can go with this', 'Or you can go with that.\r\nThis is the start of a new line and now for some bullet points:\r\n*Bullet point 1\r\n*Bullet point 2\r\nThat\'s all folks')}"></apex:outputText>


Seems a smart fellow found another neat trick here:


Sorry the solution couldn't be neater... hopefully the cause of your issue is clear though and you do have a path that will allow progression.




Hey Wes...thanks for taking a look at this. I'll experiment some more but all roads seem to point to leaning heavily on VisualForce and VisualForce templates which I avoided initially because I seem to remember reading that they couldn't be used for mass emails. My guess is that I will have to restructure my controller to include the send within a loop for each recipient.


Thanks a bunch!


Turned out to be easier to create two templates and put the selection logic in a VF controller that does the actual sending. But would have been so sweet to have been able to insert those returns into a string of text.


Thanks for the additional research Wes!

Because I struggled with something similar and google pointed to this thread, I'm adding my solution here. I wanted to show 2 lines of text in plaintext in a VF email template (2 lines, so each with a newline at the end) or nothing (so no newlines either) depending on a condition. Reading all sorts of other threads and this one, I found that:
  • {!something} always results in a newline. that's how the <apex:variable value="" var="newline"/> and then adding {!newline} thing mentioned in many places works.
  • <apex:outputText value=""/> never results in a newline. No matter what you try, when rendered as plain text newlines in the source like this:
  • value="a line
    another line"
    are always ignored, and if you add them as \n the \n goes into the output like that, while BR() results in _BR_ENCODED_ in your text.
  • The text in the value of <apex:outputText value=""/> is added after the contents of the apex:outputText tag. In other words:
  • <apex:outputText value="BB">aa</apex:outputText>
    gives aaBB, but aa is not 'processed' like value is, so it keeps its newlines if you add them.
  • apex:outputText supports the rendered attribute
Combined these may help get what you want, which for me was:
<apex:outputText rendered="{!condition}" value="">
a line
another line

When condition is false this results in :
and when condition is true this results in :
a line
another line