Why does not String.split() work in ServiceNow

abhi34
Kilo Contributor

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  .

 

find_real_file.png

13 REPLIES 13

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

Omkar Mone
Mega Sage

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

Tried your suggestion , still does not work.

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'
});