How to get line number from the Script field

peter_repan
Tera Guru

Hi all,

I have field with type "Script - Plain" on my custom table and would like to check the javascript object in the field.

For example, I would   like to get the row/line number for the property "billable" in a server-side script.

See please picture below. In this case, I would like to get number "3", because property "billable" is on the third line.

Do you have any ideas please?

Do you know if there is any server-side API for script editor?

(When you are making javascript errors, the script editor is providing you the line number, but I assume this is client-side check)

find_real_file.png

1 ACCEPTED SOLUTION

sourabhdhaygude
Kilo Guru

You can write a script on your table like below:


------------------------------------------------------------------------------------------------------------------


(function executeRule(current, previous /*null when async*/) {


  // Add your code here


  var line = "-";


  var str = current.script.split("\n");


  for(var i=0; i<str.length; i++){


  if(str[i].indexOf("How are you") > -1){


  line = i +1;



  }


  }


  gs.addInfoMessage("Found on Line number "+line);


})(current, previous);


------------------------------------------------------------------------------------------------------------------



I ran the above script on the below script file:


---------------------------------------------------------------------------------------------------------


(function executeRule(current, previous /*null when async*/) {


  // Add your code here


  gs.addInfoMessage("Hi");


  gs.addInfoMessage("Hello");


  gs.addInfoMessage("How are you");


})(current, previous);


----------------------------------------------------------------------------------------------------------



Kind regards,


Sourabh D


View solution in original post

4 REPLIES 4

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Peter,



Please check for API GlideScopedEvaluator(). More info here.


https://developer.servicenow.com/app.do#!/api_doc?v=istanbul&id=GlideEvaluator-evaluateScript_GR_S_O


Hi Pradeep,



I did not find any useful script in GlideScopedEvaluator API.


sourabhdhaygude
Kilo Guru

You can write a script on your table like below:


------------------------------------------------------------------------------------------------------------------


(function executeRule(current, previous /*null when async*/) {


  // Add your code here


  var line = "-";


  var str = current.script.split("\n");


  for(var i=0; i<str.length; i++){


  if(str[i].indexOf("How are you") > -1){


  line = i +1;



  }


  }


  gs.addInfoMessage("Found on Line number "+line);


})(current, previous);


------------------------------------------------------------------------------------------------------------------



I ran the above script on the below script file:


---------------------------------------------------------------------------------------------------------


(function executeRule(current, previous /*null when async*/) {


  // Add your code here


  gs.addInfoMessage("Hi");


  gs.addInfoMessage("Hello");


  gs.addInfoMessage("How are you");


})(current, previous);


----------------------------------------------------------------------------------------------------------



Kind regards,


Sourabh D


ramya0905
Kilo Expert

//This is run in background script - genrates URL and finds the line number of the search keyword

 

var msg = '';

var currentUrl = gs.getProperty('glide.servlet.uri');

var gr = new GlideRecord('catalog_script_client');
gr.addEncodedQuery('active!=false^scriptLIKEGlideRecord');
gr.query();
gs.print(gr.getRowCount());
while(gr.next())
{
var strtemp = gr.script;
var str = strtemp.split('\n');
var line = 0 ;
for(var i=0; i<str.length; i++)
{
line = line + 1;
if(str[i].indexOf("GlideRecord") > -1)
{
msg += "CLIENT SCRIPT : "+ gr.name + "***********" + "line : " + line + "*********" + str[i].trim() + '\n' + currentUrl+gs.generateURL('catalog_script_client', gr.sys_id) + '\n'+ '\n';
}

}
}
gs.print(msg);