I can't get new lines to appear in my APEX code as I'd expect (on a new line). I've tried every variation I can think of (\n, \n\r, <br>, <br/>, &lt;br&gt;, \\n, \\n\\r) but nothing is working, the characters just keep appearing as the literal entry (I've tried encapsulating in single and double quotes, both seem to have the same affect)

Here's my current code which generates 4 HTML elements, but no line breaks.

{!IF(Opportunity.Account.BillingStreet != '',
 Opportunity.Account.BillingStreet + '<br>' + '<br>' + "<br/>" + "<br/>",

It seems to be related to the conditional but I don't know what part, earlier in the code I have:


which gives me the account name and a real line break as I'd expect.

If more information is needed please let me know.

Here's a larger code block of what I'm working with:

<table border="0" width="100%" id="table2">
           <font face="Arial">Sample:<br/>
           <apex:repeat value="{!Opportunity.OpportunityContactRoles}" var="contact">
{!contact.Contact.Salutation} {!contact.Contact.Name} ({!contact.Contact.Email})<br/></apex:repeat>
                             {!IF(Opportunity.Account.BillingStreet != '', Opportunity.Account.BillingStreet, '')}
                             {!IF(Opportunity.Account.BillingCity != '', Opportunity.Account.BillingCity + ', ', '')}{!IF(Opportunity.Account.BillingState != '', Opportunity.Account.BillingState + ',', '')} {!Opportunity.Account.BillingPostalCode} {!Opportunity.Account.BillingCountry}<br/>
                             {!Opportunity.Account.Custom_Invoice_Text__c} {!Opportunity.Custom_Invoice_Text__c}
  • 2
    Can you post your Visualforce markup? – IllusiveBrian Jul 20 '17 at 19:37
  • @IllusiveBrian Which part specifically? The opening is <apex:page standardController="Opportunity" showHeader="false" renderas="pdf"> I've added a larger block of the code, the whole page is +100 lines – chris85 Jul 20 '17 at 19:41
  • That's fine, I didn't know if you were using this as the value of another element already, like apex:outputText in my answer. – IllusiveBrian Jul 20 '17 at 19:46
  • You should be able to use BR(), as in {!Opportunity.Account.Name & BR() }. – sfdcfox Jul 20 '17 at 20:09
  • @sfdcfox That gave me _BR_ENCODED_ – chris85 Jul 20 '17 at 20:15

In this particular case, I'd prefer to use an outputPanel:

<apex:outputPanel rendered="{!Opportunity.Account.BillingStreet != ''}" layout="none">
    <br /><br />

You can even use <apex:variable> if you want to display something when BillingStreet is blank:

<apex:variable value="{!Opportunity.Account.BillingStreet = ''}" var="streetBlank" />
<apex:outputPanel rendered="{!streetBlank}" />
<apex:outputPanel rendered="{!NOT(streetBlank)}" />

If you have a requirement specifically to display unescaped text you can use escape="false" from apex:outputText:

<apex:outputText value="{!IF(Opportunity.Account.BillingStreet != '',
 Opportunity.Account.BillingStreet + '<br>' + '<br>' + "<br/>" + "<br/>",
}" escape="false" />

Keep in mind that this allows anyone with access to BillingStreet to modify the HTML content of the page, so I would recommend against using it for this case.

| improve this answer | |
  • Be cautious when using this approach. – Adrian Larson Jul 20 '17 at 19:41
  • 1
    @AdrianLarson Yeah, I posted it and then thought better of leaving it without mentioning that. – IllusiveBrian Jul 20 '17 at 19:44
  • Can you explain what is happening currently? Is the APEX auto-encoding/escaping my elements? If I use the outputPanel approach how do I make the conditionals? – chris85 Jul 20 '17 at 19:50
  • @chris85 rendered will conditionally put the outputPanel and all of its child elements on the page, so by using it the content will only show up on the page when BillingStreet is not empty. The Visualforce renderer automatically escapes all string variables on the page (as if it had been put through the HTMLENCODE formula function). – IllusiveBrian Jul 20 '17 at 19:54
  • I'm going to try the apex:variable + <apex:outputPanel approach, with that does the new line after the element get rendered or do I need a <br> somewhere there? – chris85 Jul 20 '17 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.