Skip to main content
MarkWalsh
Inspiring
February 2, 2017
Answered

Setting RichValue with multiple line text

  • February 2, 2017
  • 2 replies
  • 2218 views

I need to set/replace some values into a richText field, and I'm having a problem with the line endings.

I am able to set the text, looping through the span objects, but I end up with a single paragraph, all of the line endings are gone.

Even just setting the value to itself, without the code to do the text replacements, doesn't work:

var spans = event.richValue;

event.richValue = spans;

or

event.richValue = event.richValue;

I end up with the correct content, but all of the line endings are missing. And this is code that I modified from the javascript reference documentation. The code is in the Custom Format Script for the field.

Can someone help me with an example of how to replace text (i.e replace '[NAME]' with 'John') in a richtext field and retain the line endings?

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

You need to use the "endParagraph" property of the span object to determine if you need to add a "\r" at the end or not. See this Stack Overflow question for some sample code:

javascript - Linespacing in multiline fields - Stack Overflow

2 replies

Karl Heinz  Kremer
Community Expert
Karl Heinz KremerCommunity ExpertCorrect answer
Community Expert
February 2, 2017

You need to use the "endParagraph" property of the span object to determine if you need to add a "\r" at the end or not. See this Stack Overflow question for some sample code:

javascript - Linespacing in multiline fields - Stack Overflow

MarkWalsh
MarkWalshAuthor
Inspiring
February 2, 2017

Thanks, I'll take a look at that, looks like exactly what I need. My solution did work, but is a bit cumbersome if it's unnecessary.

There's no mention of an 'endParagraph' property in the scripting reference file that I have, not sure why it isn't there if it works.

Thanks.

Karl Heinz  Kremer
Community Expert
Community Expert
February 2, 2017

The Javascript API documentation is not perfect, and sometimes you need to reverse engineer things a bit. In this case, you can take your "spans" information and convert it to a JavaScript code and printing it to the console using this:

console.println(spans.toSource());

You will then see all the properties of a span object.

MarkWalsh
MarkWalshAuthor
Inspiring
February 2, 2017

Ok, I've made some (debatable) progress: while looping through the spans, I can add a newline at the end of each span and that will fix the paragraph issues; but then it splits the paragraph if there are multiple spans within that paragraph (i.e. a section that is bold within the paragraph).

So, unless there's a better/more proper way to keep the line endings,  is there a way to tell if the current span is the end of the paragraph so that I can add the newline only to those spans? I'm guessing I can keep track of the character count as I loop through the spans, and check the character at the next position in the total text.

If anyone has any better suggestions, I'd be very thankful to hear them.