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

This API evaluates records with script fields defined. The scope of the script is defined by the scope of the record.

GlideScopedEvaluator - GlideScopedEvaluator()

Instantiates a GlideScopedEvaluator object.

Table 1. Parameters
Name Type Description
None

GlideScopedEvaluator - evaluateScript(GlideRecord grObj, String scriptField, Object variables)

Evaluates a script that resides in a GlideRecord field.

Table 2. Parameters
Name Type Description
grObj GlideRecord GlideRecord containing a script expression.
scriptField String Optional. Name of the field containing the script expression.
variables Object Optional. Map of variables with name-value pairs. These variables are available to the script during execution of this method.
Table 3. Returns
Type Description
Object Result of the script execution.

Example

// Setting up a record that contains the script to be executed.
var now_GR = new GlideRecord('u_global_table'); 
now_GR.u_short_description = 'Calculate Addition';  
now_GR.u_test_script = "result = x + y"; 
now_GR.insert(); 
 
var evaluator = new GlideScopedEvaluator();
evaluator.putVariable('x', 100);
evaluator.putVariable('y', 200);
gs.info(evaluator.evaluateScript(now_GR, 'u_test_script'));

Output:

300

GlideScopedEvaluator - getVariable(String name)

Returns a specified variable from a GlideScopedEvaluator object.

Table 4. Parameters
Name Type Description
name String Name of the variable to return.
Table 5. Returns
Type Description
Object Value of the specified variable.

Example

The following example shows how to call the getVariable() method to check the value of the answer variable.

(function executeRule(current, previous /*null when async*/) {
  var grAG = current.assignment_group.getRefRecord(); // Get the GlideRecord of the assignment group
  if (grAG.isValidRecord()) {
    var ge = new GlideScopedEvaluator();
    ge.putVariable("current", current); // Pass through the "current" variable as "current"
    ge.putVariable("group", grAG); // Pass through the "grAG" variable as "group"
    ge.putVariable("answer", true); // default "answer" to TRUE
    ge.evaluateScript(grAG, "u_assignment_condition"); // Run the script

    // Abort the transaction if the "answer" variable was set to FALSE explicitly (undefined doesn't count)
    if (ge.getVariable("answer") === false) { 
      gs.addErrorMessage("Assignment rule did not pass");
      current.setAbortAction(true);
    }
  }
})(current, previous);

GlideScopedEvaluator - putVariable(String name, Object value)

Sets 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 Name of the variable.
value Object Value of the variable.
Table 7. Returns
Type Description
None

Example

The following example shows how to call the putVariable() method to set the answer variable to true.

(function executeRule(current, previous /*null when async*/) {
  var grAG = current.assignment_group.getRefRecord(); // Get the GlideRecord of the assignment group
  if (grAG.isValidRecord()) {
    var ge = new GlideScopedEvaluator();
    ge.putVariable("current", current); // Pass through the "current" variable as "current"
    ge.putVariable("group", grAG); // Pass through the "grAG" variable as "group"
    ge.putVariable("answer", true); // Set default "answer" to TRUE
    ge.evaluateScript(grAG, "u_assignment_condition"); // Run the script

    // Abort the transaction if the "answer" variable was set to FALSE explicitly (undefined doesn't count)
    if (ge.getVariable("answer") === false) { 
      gs.addErrorMessage("Assignment rule did not pass");
      current.setAbortAction(true);
    }
  }
})(current, previous);