newline character on text area
Asked Answered
L

4

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

Langan answered 10/4, 2012 at 5:22 Comment(0)
S
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>
Seamanlike answered 10/4, 2012 at 14:1 Comment(7)
Im having the same problem. I tried \r\n , \n , \\n and even <br /> and non of them worked!Ylangylang
What type of tag are you using to display the data? <apex:outputfield> worked for me.Seamanlike
I cant use outputfield because I do dynamic binding. I get this error value for <apex:outputField> is not a dynamic binding!Ylangylang
Like this: <apex:outputfield value="{!Widget__c.Address__c}">.Seamanlike
I got this error: Error: Could not resolve the entity from <apex:outputField> value binding '{!fetchedData}'. <apex:outputField> can only be used with SObject fields.Ylangylang
I edited my answer, please see above. <apex:outputfield> won't work unless it is used with an sObject field.Seamanlike
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 doneLangan
K
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"/>
Kahl answered 21/8, 2013 at 15:9 Comment(0)
S
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!

Shaver answered 19/8, 2014 at 9:7 Comment(0)
C
-3

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

Change answered 21/1, 2015 at 21:50 Comment(1)
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).Ritter

© 2022 - 2024 — McMap. All rights reserved.