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

Hi Simon,



The answer to this one is to use the getVariable() method on the GlideScopedEvaluator object. See the example in the docs where they multiple x * y and return result. The calling script gets access to the result using getVariable().



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


I have tried that, but it still doesn't work for me. Here is what I have. Perhaps you can spot the gap.



The part of my script include that calls the evaluator:


if (signature) {


var sigFile = new GlideRecord('u_client_signature');


sigFile.get(signature);


if (!sigFile.isValidRecord()||JSUtil.nil(sigFile.getValue('u_script')))


return "Error";



gs.info("DEBUG: Scipt Record Found! - " + sigFile.getValue('u_script'));


var evaluator = new GlideScopedEvaluator(sigFile);


evaluator.evaluateScript(sigFile, 'u_script', null);


signature = evaluator.getVariable('result');


}


return signature;


rn signature;



Here is the script that it calls in the "sigFile" GlideRecord:


var answer = testScript();


function testScript() {


return "test";


}



Here is the output from background script when entering gs.info(new clientSignature().run());



*** Script: DEBUG: Scipt Record Found! -


var answer = testScript();


function testScript() {


return "test";


}


*** Script: null


It's not a 'return' thing, it's looking for a variable called 'result'.



Try this in your script field...



(function () {


        var result = 'Test';


})();


still null



*** Script: DEBUG: Scipt Record Found! - (function () { var result = 'Test'; })();


*** Script: null



I had previously tried similar changes. set the var answer and tried to retrieve "answer" from the evaluator.


I'm stumped.


Hi Simon,



I know it's possible. I wrote a lab for CreatorCon a couple year ago that demonstrates this... If you PM me, I can send you a link to the PDF.