How to remove line breakers from from multi line text field

venkram
Tera Contributor

below code is not working to remove line breakers on Description field. could someone help me to find out the issue.

 

On Description field is updated with -  xcgfchfg\r\n\r\ngdfhgfjfhj\r\n\r\n\r\nfdhfkghjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk\r\ndhgdh\r\n\r\n\r\n\r\n\r\n\r\nfdhfffffffffffffffffff

 

var gr = new GlideRecord('cmdb_ci_ip_firewall');
gr.addQuery('name', 'test 101');
gr.query();
if(gr.next()){
    var desc = gr.getValue('short_description');
    gs.print('Description = ' + desc);
    var res = desc.replace(/[\r\n]+/g, '');
    gr.setValue('short_description', res);
    gr.update();
}
4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@venkram 

your script looks fine and it worked for me

line break.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@venkram 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Juhi Poddar
Kilo Patron

Hello @venkram 

I agree with @Ankur Bawiskar . When we directly add the test string, it works as expected. The issue arises when retrieving the value from the short description, where it doesn't behave as anticipated.

To resolve this, I first encoded the string and then replaced the encoded values for \n and \r (%255Cn and %255Cr) with an empty string.

You can try the following script:

var gr = new GlideRecord('cmdb_ci_ip_firewall');
gr.addQuery('name', 'Test01'); // Correct query for the record
gr.query();

if (gr.next()) {
    var desc = gr.getValue('short_description');
    var encodedString = encodeURIComponent(escape(desc));
    //gs.print(encodedString);
    var cleanedDesc = encodedString.replaceAll('%255Cr', "").replaceAll('%255Cn',"");
    gs.print(cleanedDesc);
    gr.setValue('short_description', cleanedDesc);
    gr.update();
}

Result:

JuhiPoddar_0-1735984097506.png

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Hello @venkram 

I hope you are doing well.

Have you tried the solution provided by me?

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar