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
xxyyzzxxyyzz 

OutputText field line breaks

Hi,

 

I have a visualforce email template which is related to a custom object. In the template I have the following line where Agenda_Objective__c is a long text area.

 

 

<apex:outputText value="{!relatedTo.Agenda_Objective__c}"/>

 

 If there are any line breaks in the field in the custom object, they do not come through in the email template.

E.G. if the field contains the text:

 

Test1

Test2

Test3

 

the mail will display

 

Test1 Test2 Test3

 

Is there any way of preserving the line breaks? I could use outputfield instead of outputtext but this puts extra javascript into the mail which causes an error in my email client.

 

Best Answer chosen by Admin (Salesforce Developers) 
xxyyzzxxyyzz

Thanks for your help Bob. In the end I wrote a component and used the replaceAll function to replace \n with <br>

 

 

 

Note: You must set the escape attribute to false in the outputtext field of the component for this to work.

Message Edited by xxyyzz on 12-17-2009 02:51 AM

All Answers

bob_buzzardbob_buzzard

Is this an HTML or plain text template?  

 

If its HTML, this is correct behaviour, as line breaks don't have any significance in HTML.  You'd need to replace them with <BR/> and output the text unescaped.

xxyyzzxxyyzz
It is a VisualForce Template using the <messaging:htmlEmailBody > tag.
Message Edited by xxyyzz on 12-14-2009 04:09 AM
bob_buzzardbob_buzzard

Then it is behaving correctly.  Your line breaks are in there, but the email client is rendering HTML in which line breaks have no effect.

 

You'll need to replace your line breaks with <BR/> tags, which is the HTML markup for a new line.

 

 

xxyyzzxxyyzz
Thanks, I'm guessing I will have to create a component to replace line breaks with the <br/>'s as it does not seem to be possible using VisualForce functions.
bob_buzzardbob_buzzard
You might be able to use the SUBSTITUTE function to achieve this, but I'm not sure how you can represent line feed to this function.
xxyyzzxxyyzz

Thanks for your help Bob. In the end I wrote a component and used the replaceAll function to replace \n with <br>

 

 

 

Note: You must set the escape attribute to false in the outputtext field of the component for this to work.

Message Edited by xxyyzz on 12-17-2009 02:51 AM
This was selected as the best answer
BryanHartBryanHart

Sorry to drag up an old thread, but is there a better way to do this now?

I can't replace the /n with <BR/> because we need the text to be escaped for security reasons.

 

It seems like this would be a common enough problem that there'd be an output field that achieves this... like "apex:multiLineTextOutput" or an attribute or something.

protogeekprotogeek

Here's a way to do this without compromising security. Just split based on \n and then loop through the results and output with a <br /> after each line.

 

In your Apex class:

 

public List<String> getCommentsLines() {

   if (comments == null) {

        return new List<String>();

   }

   return comments.split('\n');

}

 

In your VF page:

 

<apex:repeat value="{!commentsLines}" var="cLine">
<apex:outputText value="{!cLine}" /><br />
</apex:repeat>

smoovbcaltexsmoovbcaltex

Thanks, protogeek.  Kind of a hack, but it works!

NewProgNewProg

Hi,

 

Now i too facing the same problem in <apex:outputText value="{!SpecialInstruction}"/>. This Special Instruction is a Textarea field in Account object, i'm displayig this value on the Case page.

 

If the value is entered in a new line than it should display the same in case page.

eg:

Test1

Test2

 

But now it displays as Test1 Test2

Please help on this.

bob_buzzardbob_buzzard

Is there a reason why you can't use apex:outputField?  That would retain the formatting.

ZathZath

Hi,

 

I found that you can embed the outputText inside an HTML textarea, as a quick way to get the line breaks to show correctly.  A little CSS hides the fact thats its in textarea.

Ajay_SFDCAjay_SFDC

Hi there ,

 

You can try :

<apex:outputText value="Your Text" style="white-space:pre;" /> 

It will preserve the spaces .

 

alos try <apex:outputText value="Your Text" escape="false" />  

 

Thanks 

Ajay

sunny.sfdcsunny.sfdc
I agree with bob_buzzard. 
I replaced <apex:outputText> with <apex:outputField> and it worked for me. 
Sure@DreamSure@Dream
Hi Ajay,

<apex:outputText value="Your Text" style="white-space:pre;" /> - This worked for me.

Also, the following:
<apex:outputText value="{!SUBSTITUTE(JSENCODE(Value),'\n','<br/>')}" escape="false"/>

Thanks
Sampatti Jindal 1Sampatti Jindal 1
Hi guys,
I know this is an old thread but I need help on similar grounds. My issue is with a text area field on a custom object. Using <apex:outputField> gives me the error that it can only be used on SObjects. And I'm using it in <Apex:repeat> because I'm fetching the values from my controller. Is there a way I could achieve this? All my text is rendered in a single line if I use apex:outputtext or apex:outputLabel.
Please help!
Andrew Mc.MAndrew Mc.M
Hi,

This was a useful post, with lots of different suggestions, so thanks everyone.
OutputField wasn't suitable for me in this scenario because when you view the Email Task that gets created it will contain a load of SalesForce Javascript code.
Trying the white-space:pre does work, but when the you open up the Task Email it made the page really huge, this may have had to do with my html or apex.
The following worked for me as suggested by Sure@Dream, with the addition of '\r\n' as opposed to just '\n':
<apex:outputText value="{!SUBSTITUTE(JSENCODE(Value),'\n','<br/>')}" escape="false"/>


 
Jereriah ManningJereriah Manning

This approach worked best for me. I did some replacing in Apex to create a string to turn into the line break in VF:

<apex:outputText value="{!SUBSTITUTE(JSENCODE(item), 'LBGoesHere!', '<br/>')}" style="white-space:pre;" escape="false"/>

Thanks Sure@Dream.

------------------
@Conga_Jereriah

Gabriel McGinnGabriel McGinn
FYI - 
This worked for me. The issue was objections were removing all line breaks when used in an email visual force template. 

<apex:outputField value="{!object}"/>
Tom_MillerTom_Miller
Thank you Sure@Dream, this solution is working 3 years later.

{!SUBSTITUTE(JSENCODE(ISPInformation),'\n','<br/>')}