GlideEvaluator - Global

The GlideEvaluator API allows you to evaluate scripts in a GlideRecord field from both scoped and global server scripts.

The GlideEvaluator API evaluates scripts within the script field type. The scope of the record defines the scope of the script.

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.

Scoped equivalent

To use the evaluateScript() method in a scoped application, use the corresponding scoped method: Scoped GlideEvaluator - evaluateScript(GlideRecord grObj, String scriptField, Object variables).

// For this example, we created a table: "u_global_table" with two columns: "short_description", "test_script"
// "test_script" will store the script to be evaluated by GlideScopedEvaluator.
gr = new GlideRecord('u_global_table'); 
gr.u_short_description = 'Testing GlideScopedevaluator';  
gr.u_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('u_global_table'); 
gr.addQuery('u_short_description','Testing GlideScopedevaluator'); 
gr.query(); 
if (gr.next()) { 
    gs.info(evaluator.evaluateScript(gr, 'u_test_script', vars));
}

Output: admin says hello

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.

Scoped equivalent

To use the getVariable() method in a scoped application, use the corresponding scoped method: Scoped GlideEvaluator - getVariable(String name).

//setting up a record that contains the script to be executed.
gr = new GlideRecord('u_global_table'); 
gr.u_short_description = 'Calculate Addition';  
gr.u_test_script = "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('u_global_table'); 
gr.addQuery('u_short_description','Calculate Addition'); 
gr.query(); 
if (gr.next()) { 
    evaluator.evaluateScript(gr, 'u_test_script', null);
    gs.info(evaluator.getVariable('result'));
}

Output: 300

GlideEvaluator - GlideScopedEvaluator()

Instantiates a GlideScopedEvaluator object.

Table 5. Parameters
Name Type Description
None

Scoped equivalent

To use the GlideScopedEvaluator() method in a scoped application, use the corresponding scoped method: Scoped GlideEvaluator - GlideScopedEvaluator().

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

Scoped equivalent

To use the putVariable() method in a scoped application, use the corresponding scoped method: Scoped GlideEvaluator - putVariable(String name, Object value).

//setting up a record that contains the script to be executed.
gr = new GlideRecord('u_global_table'); 
gr.u_short_description = 'Calculate Addition';  
gr.u_test_script = "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('u_global_table'); 
gr.addQuery('u_short_description','Calculate Addition'); 
gr.query(); 
if (gr.next()) { 
    evaluator.evaluateScript(gr, 'u_test_script', null);
    gs.info(evaluator.getVariable('result'));
}

Output: 300