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

Phil Swann
Tera Guru
Tera Guru

really need to know the error, but I am guessing it is due to the way servicenow handles null and empty strings... 

so just make sure before you try and split it, that you default the declaration in case it is null. 

var someText = getParm() || ''; 

someText.split() 

 

I tried what you said by doing this :

var someText = this.getParameter('sysparm_sdText').toString().split('\n').join('\\r') || '';

Did not work .

I can know the error only by putting a debugger or logging the out put .(on the server side script) .

Both of which does not work in my case ,also pointed by someone here and here.

that is not what I said to do 🙂  

 

You have tried to do it all in one line. I suggested to default the original value; and then split it on a 2nd line. 

Does not work either .