7

I have a custom field called Current_Address__c which is of datatype textarea.

I need to populate this field in the format below. ie a newline char after street and another newline after zip.

street City state Zip Country

The values of city state zip country etc are been taken from contact object. I dont want to use this as a formula field. So i need to populate it in my controller and display it on my VF page.

I am trying to add a newline char by using the code below

this.customobj.Current_Address__c = currentStreet + '\\n ' + currentCity + ' ' + currentState  + ' ' + currentZIP  + '\\n ' + currentCountry ;

i had also used \n instead of \n.

It still show the field in one line instead of 3 lines

EDIT

I got this working using the following code. I would accept mathews answer as it would work with outputfield.

                currentAddress = currentStreet;
            currentAddress += '\r\n';
            currentAddress += currentCity + + ' ' + currentState  + ' ' + currentZIP ;
            currentAddress += '\r\n';
            currentAddress += currentCountry;

This works only if you use +=. not sure why this happens

Prady
  • 10,978
  • 39
  • 124
  • 176

4 Answers4

8

I think I found the issue, you have two escape character slashes (\\n), but only one is needed because the slash in \n does not need to be escaped in this context.

Also, Salesforce saves a new line as \r\n. Try this:

this.customobj.Current_Address__c 
    = currentStreet + ' \r\n' 
    + currentCity + ' ' + currentState  + ' ' + currentZIP  + ' \r\n' 
    + currentCountry;

This method works when using an <apex:outputfield> with an sObject field.

<apex:outputtext value="{!myCustomSObject__c.Address__c}"/>

If you're using a different Visualforce Component, it won't work. Visualforce renders the new line in HTML when using a <apex:outputtext> Component, but HTML ignores new lines. If you use a <br/> tag, Visualforce renders it as &lt;br/&gt;.

The best solution I could come up with for rendering a variable that has new lines in it (rather than an sObject field) is to use a disabled <apex:inputtextarea>.

<apex:inputtextarea value="{!myAddress}" disabled="true" readonly="true">
</apex:inputtextarea>
Matt K
  • 7,207
  • 5
  • 39
  • 60
  • Im having the same problem. I tried \r\n , \n , \\n and even
    and non of them worked!
    – raym0nd Apr 10 '12 at 14:21
  • What type of tag are you using to display the data? `` worked for me. – Matt K Apr 10 '12 at 14:25
  • I cant use outputfield because I do dynamic binding. I get this error value for is not a dynamic binding! – raym0nd Apr 10 '12 at 14:37
  • Like this: ``. – Matt K Apr 10 '12 at 14:45
  • I got this error: Error: Could not resolve the entity from value binding '{!fetchedData}'. can only be used with SObject fields. – raym0nd Apr 10 '12 at 14:57
  • 1
    I edited my answer, please see above. `` won't work unless it is used with an sObject field. – Matt K Apr 10 '12 at 15:33
  • 2
    Mathew.. i had tried a similar code before i posted here. i had used /r/n and with single / . It didnt come out correct. it was all coming up correct. As raymOnd had used i was also using outputtext. I finally code got it working on outputtext. i am edit my quesstion to post the code i had done – Prady Apr 13 '12 at 02:55
3

Recently I had the same problem, I wanted to reder the new lines in an The solution that I found was this, it is a little tricky but it works:

<apex:outputText value="{!SUBSTITUTE(JSENCODE(textVariableThanContainsNewLines), '\\n', '<br/>')}" escape="false"/>
0

Try this:

Controller

public List<String> getLetterLines() {
    if (letterBody == null) {
        return new List<String>();
    }
    return letterBody.split('\n');
}

VF page:

<apex:repeat value="{!letterLines}" var="letterLine">
    <apex:outputText value="{!letterLine}" /><br />
</apex:repeat>

Have fun!

sskular
  • 240
  • 2
  • 5
-3

value="Remarks : {!SUBSTITUTE(JSENCODE(textVariableThanContainsNewLines), '\r\n', '
')}"

  • 3
    Some things to think about before answering: 1) Your answer should **add** new information to the post, not just duplicate someone else's, 2) You should explain why/how the answer solves the problem, and 3) You should format your answer nicely (put code in code blocks). – Ajean Jan 21 '15 at 21:59