execute script in custom script field

NeilH2
Giga Guru

Hi

I'm writing a new application and i want to add a cutom script field to a table that a workflow will locate via a glideRecord.

The idea is the workflow will execute the script.

I'm just a little lost on how to tell ServiceNow to execute a script in a script field on a custom table.

I expected a command like "gr.u_script.run" but cant find any reference.

Any suggestions welcome

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Neil,



Check the class GlideScopedEvaluator and the method evaluateScript.



I use it all the time.


View solution in original post

11 REPLIES 11

Chuck Tomasi
Tera Patron

Hi Neil,



Check the class GlideScopedEvaluator and the method evaluateScript.



I use it all the time.


Hi Chuck



Would the script i reference be able to use the "current." or do i need to pass it in as a variable?


I ended up passing the current value in this allowed me to grab the current.varibales as well.



for those interested


where gr. is the referenced GlideRecord.



var evaluator = new GlideScopedEvaluator();


evaluator.putVariable(current);


evaluator.evaluateScript(gr, 'u_run_script', null);



Thanks Chuck



Regards


Neil


I'm trying to use GlideScopedEvaluator for the first time, and there is a specific point I am looking for clarification on that the documentation doesn't talk about. I wondered if you might enlighten me.



When using this evaluator, how am I supposed to be returning the information from the evaluated script? I first tried just using return "XYZ";. When that didn't work, I tried setting my desired return result to var answer = "XYZ". I've also just tried putting something like gs.info("TEST"); in the script to see if that logs anything, but nothing seems to be working. I just keep getting undefined.




If I'm not making sense, here is what I am going for...


I have mail script that run and calls a particular script include function to get the details of what the mail script is going to print out with template.print(MY SCRIPT INCLUDE);


This part I have confirmed is fully working. it only started to break when I included the evaluator into the mix.


I have setup a custom table with just 2 custom fields; u_name, u_script.


The script include has logic that will get it to identify a particular record on this custom table, I then want to return the result of the script field on this custom table through the script include to be printed out with the mail script.


Let me know if you need me to expand further on my explanation.



Any feedback would be helpful. Thanks!