+ Start a Discussion
Anand@SAASAnand@SAAS 

Line breaks in Visual force email template that has <plainText>

I want to have a newline in a visual force email template. Here's what the template looks like:

 

<messaging:emailTemplate subject="{!$Label.Action_Alert_Subject}" recipientType="User" language="{!recipient.LanguageLocaleKey}" relatedToType="User">
<messaging:plainTextEmailBody >
<apex:repeat value="{!relatedTo.Action_Alerts__r}" var="alert">
<apex:outputText value="{!alert.Type}({!alert.Count__c}) ;" rendered="{!alert.Type__c='Orders Not Submitted'}"/>
<apex:outputText value="{!$Label.Orders_below_Minimum}({!alert.Count__c}) ;" rendered="{!alert.Type__c='Orders Under Minimum'}"/>

</apex:repeat>
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

 

 

If I have two record in "Action Items", i expect the two records in two separate lines. However it does'nt get sent that way. If I use the following:

 

<messaging:emailTemplate subject="{!$Label.Action_Alert_Subject}" recipientType="User" language="{!recipient.LanguageLocaleKey}" relatedToType="User">
<messaging:plainTextEmailBody >
<apex:repeat value="{!relatedTo.Action_Alerts__r}" var="alert">
{!alert.Type__c}({!alert.Count__c};
</apex:repeat>
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

The email does have a newline after every record. I cannot use this option as the Type is a english picklist value and I need to be able to send the email in different languges based on the recipient's language preference.

 

Best Answer chosen by Admin (Salesforce Developers) 
aperezSFDCaperezSFDC

If this was an HTML email, I would suggest to use <br />, since it is plain text, you should use \r\n unfortunately if you put it in the VisualForce email template it will be treated as \\r\\n. You need to set this value in Apex, but EmailTemplates can't have extensions, so you need a VisualForce component...There has to be a simpler way ;-)

 

This is what I did:

 

Email Template:

 

<messaging:emailTemplate subject="NewLines" recipientType="Contact" relatedToType="Account">

<messaging:plainTextEmailBody >

    <apex:repeat value="{!relatedTo.Contacts}" var="c">

        <apex:outputText value="{!c.ID}" />

        <c:newline />

    </apex:repeat>

</messaging:plainTextEmailBody>

</messaging:emailTemplate>

 

Component:

 

<apex:component controller="NewLine" access="global" >

    <apex:outputText value="{!NewLine}" />

</apex:component>

 

Controller:

 

public class NewLine {

    public String NewLine {

        get { return '\r\n'; }

        set;

    }

}

All Answers

aperezSFDCaperezSFDC

If this was an HTML email, I would suggest to use <br />, since it is plain text, you should use \r\n unfortunately if you put it in the VisualForce email template it will be treated as \\r\\n. You need to set this value in Apex, but EmailTemplates can't have extensions, so you need a VisualForce component...There has to be a simpler way ;-)

 

This is what I did:

 

Email Template:

 

<messaging:emailTemplate subject="NewLines" recipientType="Contact" relatedToType="Account">

<messaging:plainTextEmailBody >

    <apex:repeat value="{!relatedTo.Contacts}" var="c">

        <apex:outputText value="{!c.ID}" />

        <c:newline />

    </apex:repeat>

</messaging:plainTextEmailBody>

</messaging:emailTemplate>

 

Component:

 

<apex:component controller="NewLine" access="global" >

    <apex:outputText value="{!NewLine}" />

</apex:component>

 

Controller:

 

public class NewLine {

    public String NewLine {

        get { return '\r\n'; }

        set;

    }

}

This was selected as the best answer
Ralph CallawayRalph Callaway

Very cool solution.  You can accomplish the same thing by including two spaces in front of the line where you want the new line.  I got this idea from this wiki post about visualforce email templates: http://wiki.developerforce.com/index.php/VisualForceEmailTemplates_sample?action=edit

 

Here's the example they use which renders a new line instead of the spaces before [ Case Number ] and [ {!cx.CaseNumber} ]

 

<pre>
<messaging:plainTextEmailBody >
Dear {!recipient.name},
 
Below is a list of cases related to Account: {!relatedTo.name}

              [ Case Number ] - [ Subject ] - [ Email ] - [ Status ]

<apex:repeat var="cx" value="{!relatedTo.Cases}">
              [ {!cx.CaseNumber} ] - [ {!cx.Subject} ] - [ {!cx.Contact.email} ] - [ {!cx.Status} ]
</apex:repeat>

 For more detailed information login to http://www.salesforce.com
</messaging:plainTextEmailBody>    
</pre>

AnSFAdminAnSFAdmin
Because I struggled with something similar and everything points 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.
  • apex:outputText supports the rendered attribute
Combined these may help get what you want, which for me was:
X
<apex:outputText rendered="{!condition}" value="">
a line
another line
</apex:outputText>  
X

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