Why does not String.split() work in ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2020 04:16 PM
I am an experienced Java Script developer who is new to Service Now . I need to replace '\n' with '\r' in the short_description field in the incident table .
The line of the code , that does the job for me in the vanilla JavaScript is :
someText = someText.split("\n").join("\\r");
However it does not work in ServiceNow .
I also borrowed the idea from a similar question here :ServiceNowDev_Questions but it does not work either.
My Script Include : is :
var ServiceNow201GlideAjax = Class.create();
ServiceNow201GlideAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
replaceNewline: function() {
someText = this.getParameter('sysparm_sdText').split('\n').join('\\r');
return someText;
//var newString = this.getParameter('sysparm_sdText').replace('\n', '\r');
//return this.getParameter('sysparm_sdText').replace(/\r\n|\r|\n/g, '\\r');;
},
type: 'ServiceNow201GlideAjax'
});
And the client script is :
function onLoad() {
//Type appropriate comment here, and begin script below
var sdText = g_form.getValue('short_description');
var ga = new GlideAjax("ServiceNow201GlideAjax");
ga.addParam('sysparm_name' ,'replaceNewline');
ga.addParam('sysparm_sdText' , sdText);
ga.getXML(callback);
}
function callback(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('short_description',answer);
}
So how do i make it work ?
Edit :
Just to confirm the working of the logic used in script include . Please see below the screenshot which contains a vanila java script code giving the output as expected .
- Labels:
-
Personal Developer Instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2020 07:21 PM
Hi Abhi,
possibly try to use toString() and then split
someText = this.getParameter('sysparm_sdText').toString().split('\n').join('\\r');
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2020 08:30 PM
Hi
Maybe try this once -
var ServiceNow201GlideAjax = Class.create();
ServiceNow201GlideAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
replaceNewline: function() {
var someText = this.getParameter('sysparm_sdText').toString().split('\n').join('\\r');
return someText.toString();
//var newString = this.getParameter('sysparm_sdText').replace('\n', '\r');
//return this.getParameter('sysparm_sdText').replace(/\r\n|\r|\n/g, '\\r');;
},
type: 'ServiceNow201GlideAjax'
});
Hope this helps.
Regards
Omkar Mone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2020 09:08 PM
Tried your suggestion , still does not work.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2020 09:39 PM
Hi abhi,
This worked in my background script properly. Try this once as well, i have put a log there, let me know what does typeof return you -
var ServiceNow201GlideAjax = Class.create();
ServiceNow201GlideAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
replaceNewline: function() {
var someText = this.getParameter('sysparm_sdText').toString();
gs.log(" Type of this is - "+ typeof someText);
var someText2 = someText.split('\n').join('\\r');
return someText2.toString();
//var newString = this.getParameter('sysparm_sdText').replace('\n', '\r');
//return this.getParameter('sysparm_sdText').replace(/\r\n|\r|\n/g, '\\r');;
},
type: 'ServiceNow201GlideAjax'
});