How can I run a script stored in a "Script" type field, from another script?

bcronrath
Kilo Guru

My goal here is to run the script contained within a field which I'm grabbing in a glideRecord for a scheduled job.   I'm not having much luck searching through docs because every time I search for script related queries it just comes up with a plethora of other scripting articles.   I can see the script type listed here Introduction to Fields - ServiceNow Wiki but it doesn't really tell me how I am suppose to run them from other areas of the system unfortunately.

Essentially I want a scheduled job to run script like this:

var gl = new GlideRecord('table');

gl.query();

while(gl.next()){

        //Run gl.u_script (where u_script is a "script" field type)

}

Is it possible to do this?   I have a schedule job I want to run but the script is unique to each row in this table.  

Best regards,

Brian

1 ACCEPTED SOLUTION

bcronrath
Kilo Guru

Hey guys, found what I was looking for, looks like it is this evaluate function:



var gl = new GlideRecord('table');


gl.query();


while(gl.next()){


  var evaluator = new GlideScopedEvaluator();


  gs.info(evaluator.evaluateScript(gl, 'u_script', ''));


}



Works like a charm!


View solution in original post

7 REPLIES 7

jcote
Giga Expert

Hi Brian,



I think you're looking for a Script Include in ServiceNow. It's a Server Side library that you can call functions from. Be aware of naming conventions when you name the script include and when you call it. Hope this helps.



Script Include:


var NewInclude =Class.create();



NewInclude.prototype = {


  initialize : function() {


    },



  myFunction : function() {


    //Put function code here


    },



  type : 'NewInclude'


};



Schedule Job:


var foo =new NewInclude();


foo.myFunction();




More information on Script Includes can be found here in the ServiceNow wiki: Script Includes - ServiceNow Wiki


Thank you Jamison,


It's not quite what I'm looking for but a similar idea.   Basically what I'm trying to do is utilize the "script" type field that can be defined for fields.   Currently, I'm not sure how we are supposed to be able to utilize these fields.   I'm assuming something exists in Service-now along the lines of a "call" type method or function for these but I've been unable to find them so far.



To illustrate further what I'm trying to accomplish - a script include could essentially serve the same purpose that my scheduled job is trying to do.  



Script Include:




  1. var NewInclude =Class.create();  
  2.  
  3. NewInclude.prototype = {  
  4.   initialize : function() {  
  5.     },  
  6.  
  7.   myFunction : function() {  
  8.    

var gl = new GlideRecord('table');


gl.query();


while(gl.next()){


        //Run gl.u_script (where u_script is a "script" field type)


}


  1.     },  
  2.  
  3.   type : 'NewInclude'  
  4. };  



Hopefully this makes sense in what I'm trying to do


yetesh_ch
ServiceNow Employee
ServiceNow Employee

Hi,


Can you try to insert a new script include using GlideRecord and script field of your table. You can then do like:


var foo = new NewInclude();


foo.myFunction();



where NewInclude is name of inserted script include and myFunction() is function you wanna execute.



Hope it helps!


bcronrath
Kilo Guru

Hey guys, found what I was looking for, looks like it is this evaluate function:



var gl = new GlideRecord('table');


gl.query();


while(gl.next()){


  var evaluator = new GlideScopedEvaluator();


  gs.info(evaluator.evaluateScript(gl, 'u_script', ''));


}



Works like a charm!