How to preserve line breaks when switching a field from String to HTML

Stefan Reichelt
Tera Guru
Tera Guru

When I change an existing String field (like Description or Resolve Notes) from String to HTML, all the line breaks within existing entries get lost.

What's the best way to prevent that? Do I have to run some update script over the whole table before converting, or is there an easier way?

1 ACCEPTED SOLUTION

Yes, correct, you have to perform a background script (possibly during the no-peak hours to avoid performance issue) where you will massively update the table using string.replace().

This is the best thing to do for having a good result, give a try and let me know if it works!

Cheers

Alberto

View solution in original post

5 REPLIES 5

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi,

try the solution suggested by Matt here:

https://community.servicenow.com/community?id=community_question&sys_id=41e5a78cdbf77bc8fff8a345ca96...

If I have answered your question, please mark my response as correct and/or helpful.

Thank you very much

Cheers
Alberto

Thanks for your answer. I also found that article, but it's about a UI action for a single entry. I want to switch the whole field type from String to HTML, so I can e.g. enter Resolution Notes both in Rich Text and with embedded pictures.

Does it mean I would have to convert all existing text entries with String.replace() manually on the table? If so, what's the best way to do so with a minimum impact on the overall system performance?

Br

Yes, correct, you have to perform a background script (possibly during the no-peak hours to avoid performance issue) where you will massively update the table using string.replace().

This is the best thing to do for having a good result, give a try and let me know if it works!

Cheers

Alberto

Ok, thank you very much. My script (below) works fine, although the performance was horrible. Not sure if it makes sense to run that in an environment with millions of lines, but if I ever want to switch the field in production, this may be the only way.

grTask = new GlideRecord('task');
grTask.addQuery('close_notes','!=','');
grTask.query();
while (grTask.next()){
grTask.close_notes = grTask.close_notes.replace(/\n/g, '<br></br>');
grTask.update();
}