---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# GlideScopedEvaluator - Scoped

# GlideScopedEvaluator - Scoped {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 2 minutes to read

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() {#ariaid-title2}

Instantiates a GlideScopedEvaluator object.
{#r_ScopedGlideEvaluatorGlideScopedEvaluator__table_twg_dgr_2r__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 1. Parameters]

{#r_ScopedGlideEvaluatorGlideScopedEvaluator__table_twg_dgr_2r}

## GlideScopedEvaluator - evaluateScript(GlideRecord grObj, String scriptField, Object
variables) {#ariaid-title3}

Evaluates a script that resides in a GlideRecord field.
{#r_ScopedGlideEvaluatorEvaluateScript_GlideRecord_String_Object__table_l3r_hch_1r__entry__3}

| Name | Type | Description |
|-|-|-|
| grObj | [GlideRecord](https://servicenow-prod.fluidtopics.net/Z64TK~kWnpWTEG5bCPvZuQ#c_GlideRecordScopedAPI "The scoped GlideRecord API is used for database operations.") | 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 2. Parameters]

{#r_ScopedGlideEvaluatorEvaluateScript_GlideRecord_String_Object__table_l3r_hch_1r} {#r_ScopedGlideEvaluatorEvaluateScript_GlideRecord_String_Object__table_bdf_zfr_2r__entry__2}

| Type | Description |
|-|-|
| Object | Result of the script execution. |
[Table 3. Returns]

{#r_ScopedGlideEvaluatorEvaluateScript_GlideRecord_String_Object__table_bdf_zfr_2r}  

    // 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) {#ariaid-title4}

Returns a specified variable from a GlideScopedEvaluator object.
{#r_ScopedGlideEvaluatorGetVariable_String__table_e3v_4dh_1r__entry__3}

| Name | Type | Description |
|-|-|-|
| name | String | Name of the variable to return. |
[Table 4. Parameters]

{#r_ScopedGlideEvaluatorGetVariable_String__table_e3v_4dh_1r} {#r_ScopedGlideEvaluatorGetVariable_String__table_v5f_hgr_2r__entry__2}

| Type | Description |
|-|-|
| Object | Value of the specified variable. |
[Table 5. Returns]

{#r_ScopedGlideEvaluatorGetVariable_String__table_v5f_hgr_2r}  
The following example shows how to call the getVariable() method to check the value of the <var class="keyword varname">answer</var> 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) {#ariaid-title5}

Sets a variable into the GlideScopedEvaluator object. These variables are available to the script that this GlideScopedEvaluator object runs.
{#r_ScopedGlideEvaluatorPutVariable_String_Object__table_e11_f2h_1r__entry__3}

| Name | Type | Description |
|-|-|-|
| name | String | Name of the variable. |
| value | Object | Value of the variable. |
[Table 6. Parameters]

{#r_ScopedGlideEvaluatorPutVariable_String_Object__table_e11_f2h_1r} {#r_ScopedGlideEvaluatorPutVariable_String_Object__table_hpj_bgr_2r__entry__2}

| Type | Description |
|-|-|
| None |   |
[Table 7. Returns]

{#r_ScopedGlideEvaluatorPutVariable_String_Object__table_hpj_bgr_2r}  
The following example shows how to call the putVariable() method to set the <var class="keyword varname">answer</var> 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);


