GlideEvaluator - Scoped

The GlideScopedEvaluator API allows you to evaluate scripts from a GlideRecord field.

Scoped GlideEvaluator - evaluateScript(GlideRecord grObj, String scriptField, Object variables)

Evaluates a script from a GlideRecord field.

Table 1. Parameters
Name Type Description
grObj GlideRecord The GlideRecord containing a script expression.
scriptField String (Optional) The name of the field containing the script expression.
variables Object (Optional) A map of variables with name-value pairs. These variables are available to the script during execution of this method.
Table 2. Returns
Type Description
Object The result of the script execution.
// For this example, we created a table: "x_app_table" with two columns: "short_description", "test_script"
// "test_script" will store the script to be evaluated by GlideScopedEvaluator.
gr = new GlideRecord('x_app_table'); 
gr.short_description = 'Testing GlideScopedevaluator';  
gr.test_script = "gs.getUser().getName() + ' says ' + greeting; "; 
gr.insert(); 
 
// setup variables to be used by the script
var vars = {'greeting' : 'hello'};
 
//Evaluate the script from the field
var evaluator = new GlideScopedEvaluator(); 
gr = new GlideRecord('x_app_table'); 
gr.addQuery('short_description','Testing GlideScopedevaluator'); 
gr.query(); 
if (gr.next()) { 
    gs.info(evaluator.evaluateScript(gr, 'test_script', vars));
}

Output: admin says hello

Scoped GlideEvaluator - getVariable(String name)

Gets a variable from a GlideScopedEvaluator object.

Table 3. Parameters
Name Type Description
name String The name of the variable.
Table 4. Returns
Type Description
Object The value of the specified variable.
//setting up a record that contains the script to be executed.
gr = new GlideRecord('x_app_table'); 
gr.short_description = 'Calculate Addition';  
gr.calculate = "result = x + y"; 
gr.insert(); 
 
var evaluator = new GlideScopedEvaluator();
evaluator.putVariable('x', 100);
evaluator.putVariable('y', 200);
evaluator.putVariable('result', null);
 
// Now retrieve the result 
gr = new GlideRecord('x_app_table'); 
gr.addQuery('short_description','Calculate Addition'); 
gr.query(); 
if (gr.next()) { 
    evaluator.evaluateScript(gr, 'calculate', null);
    gs.info(evaluator.getVariable('result'));
}

Output:

Scoped GlideEvaluator - GlideScopedEvaluator()

Instantiates a GlideScopedEvaluator object.

Table 5. Parameters
Name Type Description
None

      

Output:

Scoped GlideEvaluator - putVariable(String name, Object value)

Puts a variable into the GlideScopedEvaluator object. These variables are available to the script that this GlideScopedEvaluator object runs.

Table 6. Parameters
Name Type Description
name String The name of the variable.
value Object The value of the variable.
Table 7. Returns
Type Description
void
/setting up a record that contains the script to be executed.
gr = new GlideRecord('x_app_table'); 
gr.short_description = 'Calculate Addition';  
gr.calculate = "result = x + y"; 
gr.insert(); 
 
var evaluator = new GlideScopedEvaluator();
evaluator.putVariable('x', 100);
evaluator.putVariable('y', 200);
evaluator.putVariable('result', null);
 
// Now retrieve the result 
gr = new GlideRecord('x_app_table'); 
gr.addQuery('short_description','Calculate Addition'); 
gr.query(); 
if (gr.next()) { 
    evaluator.evaluateScript(gr, 'calculate', null);
    gs.info(evaluator.getVariable('result'));
}

Output: 300