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

Does not work mate.

Barrilito van D
Kilo Guru

Hi abhi.

In order to look into your issue well, I took the assumption that the call to the script include works and indeed the only issue is the split. In general when posting a question it will always help if you share the outcome of (in this case) the split. A sentence with "it does not work" does not tell me what it does or does not do.

In general, I would add .toString() in the script include to your string to be sure it is explicitly casted to a string, as you want to do a string split.

When you have done so, just log it first to be sure if the call you do to the include will indeed print a correct string in the log.

Being sure of that, do the split. And that should create an array. Does it create an array? Or anything? Can you share us what the result is?  Try printing out yourstring[0], yourstring[1] etc. It should work. With the toString() added to it.

Then if that would work the join will melt the array parts back into a string.

In steps this would be how it would test it. If I don't know the step answers it will be hard to help. Can you let us know the steps how they work out?

Thanks!

 


 

If you think my answer put you in the right direction or you appreciated my effort, please mark the response as Helpful (👍). That way other people in the community will probably be triggered to read the responses too. If my answer solved your issue, please mark my response as Correct (). In the list of question people like me that want to help out will know your question was already answered and others that are curious to the answer know a good answer is given in the thread.
Thanks in advance! Good luck!

Hi Barrilito van Dijk

 

Thanks for a comprehensive reply .
Yes my script include works well . I tested this by returning just a random string from the script include .

Debugging :
I have done some fair bit of development on JavaScript , and to debug the server side code , i would simply put a server side debugger using some external plugin or just do a console.log() .

I tried debugging it on Service Now , following the video here , but the debugger won't pause on the breakpoints .I am using the Madrid instance and someone else has raised this issue as well here .

The gs.log() or gs.info() wont work either in my case, as also pointed by someone here.

 

In the client script however , i tried printing the  response  as :

   

jslog("response : ",response.responseXML.documentElement.getAttribute('answer'));   

and it would simply log  :
response :

.

 

Thanks

 

 

Ian Mildon
Tera Guru

Have you tried doing this the other way and perform the split/join from the Client Script, passing the string as a parameter to the Script Include?

I have a Client Script/Script Include pair that successfully splits a URL to extract a sys_id, confirms a value within the identified record and then returns the string as a URL in a new field.

I did not understand your suggestion to entirety but drawing some idea  from your suggestion .
I used a standalone client script to test my function :
Here is the standalone client script :

function onLoad() {
  var sdText = g_form.getValue("short_description");
  
  var someText = sdText
    .toString()
    .split("\n")
    .join("\\r");
	jslog("Hello world !");
	jslog(someText.toString());
	

    g_form.setValue('short_description',someText);
}

And in the console i saw "AA Created by \n Abhi\n \n' which is indeed the sdText.

 

The similar script on running on Vanilla JavaScript gave :

find_real_file.png

 

which is what i need .