{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1012,"path":"api-reference/server-api-reference/c_GlideEvaluatorScopedAPI.md","title":"GlideScopedEvaluator - Scoped","bundle":null,"doc_type":null,"release":"australia","body":"\n# GlideScopedEvaluator- Scoped\n\nThe GlideScopedEvaluator API allows you to evaluate scripts in a GlideRecord field from both scoped and global server scripts.\n\nThis API evaluates records with script fields defined. The scope of the script is defined by the scope of the record.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## GlideScopedEvaluator - GlideScopedEvaluator\\(\\)\n\nInstantiates a GlideScopedEvaluator object.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n## GlideScopedEvaluator - evaluateScript\\(GlideRecord grObj, String scriptField, Object variables\\)\n\nEvaluates a script that resides in a GlideRecord field.\n\n|Name|Type|Description|\n|----|----|-----------|\n|grObj|[GlideRecord](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideRecordScopedAPI.md)|GlideRecord containing a script expression.|\n|scriptField|String|Optional. Name of the field containing the script expression.|\n|variables|Object|Optional. Map of variables with name-value pairs. These variables are available to the script during execution of this method.|\n\n|Type|Description|\n|----|-----------|\n|Object|Result of the script execution.|\n\n```\n// Setting up a record that contains the script to be executed.\nvar now_GR = new GlideRecord('u_global_table'); \nnow_GR.u_short_description = 'Calculate Addition';  \nnow_GR.u_test_script = \"result = x + y\"; \nnow_GR.insert(); \n \nvar evaluator = new GlideScopedEvaluator();\nevaluator.putVariable('x', 100);\nevaluator.putVariable('y', 200);\ngs.info(evaluator.evaluateScript(now_GR, 'u_test_script'));\n```\n\nOutput:\n\n```\n300\n```\n\n## GlideScopedEvaluator - getVariable\\(String name\\)\n\nReturns a specified variable from a GlideScopedEvaluator object.\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|Name of the variable to return.|\n\n|Type|Description|\n|----|-----------|\n|Object|Value of the specified variable.|\n\nThe following example shows how to call the getVariable\\(\\) method to check the value of the *answer* variable.\n\n```\n(function executeRule(current, previous /*null when async*/) {\n  var grAG = current.assignment_group.getRefRecord(); // Get the GlideRecord of the assignment group\n  if (grAG.isValidRecord()) {\n    var ge = new GlideScopedEvaluator();\n    ge.putVariable(\"current\", current); // Pass through the \"current\" variable as \"current\"\n    ge.putVariable(\"group\", grAG); // Pass through the \"grAG\" variable as \"group\"\n    ge.putVariable(\"answer\", true); // default \"answer\" to TRUE\n    ge.evaluateScript(grAG, \"u_assignment_condition\"); // Run the script\n\n    // Abort the transaction if the \"answer\" variable was set to FALSE explicitly (undefined doesn't count)\n    if (ge.getVariable(\"answer\") === false) { \n      gs.addErrorMessage(\"Assignment rule did not pass\");\n      current.setAbortAction(true);\n    }\n  }\n})(current, previous);\n```\n\n## GlideScopedEvaluator - putVariable\\(String name, Object value\\)\n\nSets a variable into the GlideScopedEvaluator object. These variables are available to the script that this GlideScopedEvaluator object runs.\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|Name of the variable.|\n|value|Object|Value of the variable.|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to call the putVariable\\(\\) method to set the *answer* variable to true.\n\n```\n(function executeRule(current, previous /*null when async*/) {\n  var grAG = current.assignment_group.getRefRecord(); // Get the GlideRecord of the assignment group\n  if (grAG.isValidRecord()) {\n    var ge = new GlideScopedEvaluator();\n    ge.putVariable(\"current\", current); // Pass through the \"current\" variable as \"current\"\n    ge.putVariable(\"group\", grAG); // Pass through the \"grAG\" variable as \"group\"\n    ge.putVariable(\"answer\", true); // Set default \"answer\" to TRUE\n    ge.evaluateScript(grAG, \"u_assignment_condition\"); // Run the script\n\n    // Abort the transaction if the \"answer\" variable was set to FALSE explicitly (undefined doesn't count)\n    if (ge.getVariable(\"answer\") === false) { \n      gs.addErrorMessage(\"Assignment rule did not pass\");\n      current.setAbortAction(true);\n    }\n  }\n})(current, previous);\n```\n\n"}}