function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DJP1SDJP1S 

Retain Formatting when copying Long Text Area to Rich Text Area

I've got a field that is long text that I would like to copy over to rich text in a trigger. The text gets brought over just fine, but it loses any carriage returns. If I make the Rich Text Field a Long Text Field, I easily keep the formatting by assigning

 

ObjectA.FieldA = ObjectB.FieldB;

 

 

Here's what I get now...

 

Long text field:

Blah Blah Blah

Neat Neat Neat

Oh So Cool

 

Rich Text Field:

Blah Blah BlahNeat Neat NeatOh So Cool

 

How can I keep my carriage returns? I would rather not have to change the field types, that is a last-ditch effort.

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Oops, thats not so cool.
You need to replace carriage returns with br's as rich text area accepts HTML instead of string. So it doesn't identify \n or new lines.

 

try something like this:

ObjectA.FieldA = ObjectB.FieldB.replaceAll('\n', '<br/>');

All Answers

Rahul SharmaRahul Sharma

Oops, thats not so cool.
You need to replace carriage returns with br's as rich text area accepts HTML instead of string. So it doesn't identify \n or new lines.

 

try something like this:

ObjectA.FieldA = ObjectB.FieldB.replaceAll('\n', '<br/>');
This was selected as the best answer
DJP1SDJP1S

Rahul Sharma wrote:

 

try something like this:

ObjectA.FieldA = ObjectB.FieldB.replaceAll('\n', <br/>);

 

Thanks! I added '' to <br/> and it works fine.

 

ObjectA.FieldA = ObjectB.FieldB.replaceAll('\n', '<br/>');