Explore Courses Blog Tutorials Interview Questions
0 votes
in Salesforce by (11.9k points)

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 shows the field in one line instead of 3 lines


1 Answer

+1 vote
by (32.1k points)
selected by
Best answer

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

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

currentAddress = currentStreet;

            currentAddress += '\r\n';

            currentAddress += currentCity + + ' ' + currentState  + ' ' + currentZIP ;

            currentAddress += '\r\n';

            currentAddress += currentCountry;

Browse Categories