Newline in text area

I have a custom field Current_Address__c that has a textarea data type.

I need to fill out this field in the following format. those. a new char line after the street and another new line after the zip.

Street City State Zip Country

Values ​​of urban postal country, etc. taken from the contact object. I do not want to use this as a formula field. Therefore, I need to fill it in my controller and display it on my VF page.

I am trying to add a new char line using the code below

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

i also used \ n instead of \ n.

It still shows the field in one line instead of three lines

EDIT

I got this working using the following code. I would agree with mathews answer as it will work with the output field.

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

This only works if you use + =. don't know why this is happening

+6
source share
4 answers

I think I found the problem, you have two escape-character slashes ( \\n ), but only one is needed, because in this context no slash is needed in \n .

In addition, Salesforce saves the new line as \r\n . Try the following:

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

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

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

If you use another component of Visualforce, this will not work. Visualforce displays a newline in HTML when using the <apex:outputtext> component, but HTML ignores newlines. If you use the <br/> tag, Visualforce displays it as &lt;br/&gt; .

The best solution I could offer for rendering a variable with new lines in it (and not with the sObject field) is to use the disabled <apex:inputtextarea> .

 <apex:inputtextarea value="{!myAddress}" disabled="true" readonly="true"> </apex:inputtextarea> 
+7
source

Lately, I had the same problem, I wanted to rewrite new lines in the Solution I found was this, this is a bit complicated, but it works:

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

Try the following:

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> 

Good luck

0
source

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

-2
source

Source: https://habr.com/ru/post/912814/


All Articles