You need to sign in to do that
Don't have an account?
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.
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.
All Answers
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.
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.
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.
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.
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>
Thanks, protogeek. Kind of a hack, but it works!
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.
Is there a reason why you can't use apex:outputField? That would retain the formatting.
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.
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
I replaced <apex:outputText> with <apex:outputField> and it worked for me.
<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
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!
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"/>
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
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}"/>
{!SUBSTITUTE(JSENCODE(ISPInformation),'\n','<br/>')}