{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":793,"path":"api-reference/server-api-reference/GlideEvaluatorAPI.md","title":"GlideScopedEvaluator - Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# GlideScopedEvaluator- Global\n\nThe GlideScopedEvaluator API allows you to evaluate scripts in a GlideRecord field from both scoped and global server scripts.\n\nThis API evaluates scripts within the script field type. The scope of the record defines the scope of the script.\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### Scoped equivalent\n\nTo use the GlideScopedEvaluator\\(\\) method in a scoped application, use the corresponding scoped method: [GlideScopedEvaluator\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideEvaluatorScopedAPI.md).\n\n## GlideScopedEvaluator - evaluateScript\\(GlideRecord grObj, String scriptField, Object variables\\)\n\nEvaluates a script from a GlideRecord field.\n\n|Name|Type|Description|\n|----|----|-----------|\n|grObj|GlideRecord|The GlideRecord containing a script expression.|\n|scriptField|String|\\(Optional\\) The name of the field containing the script expression.|\n|variables|Object|\\(Optional\\) A 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|The result of the script execution.|\n\n```\n//For this example, we created a table: \"x_app_table\" with two columns: \"short_description\", \"test_script\"\n// \"test_script\" will store the script to be evaluated by GlideScopedEvaluator.\nvar now_GR = new GlideRecord('x_app_table');\nnow_GR.short_description = 'Testing GlideScopedEvaluator';\nnow_GR.test_script = \"gs.getUser().getName() + ' says ' + greeting; \";\nnow_GR.insert();\n\n//setup variables to be used by the script\nvar vars = {'greeting' : 'hello'};\n\n//Evaluate the script from the field\nvar evaluator = new GlideScopedEvaluator();\ngs.info(evaluator.evaluateScript(now_GR, 'test_script', vars));\n\n// Now retrieve the result\nevaluator.evaluateScript(gr, 'u_test_script', null);\ngs.info(evaluator.getVariable('result'));\n```\n\nOutput:\n\n```\nadmin says hello\n```\n\n### Scoped equivalent\n\nTo use the evaluateScript\\(\\) method in a scoped application, use the corresponding scoped method: [evaluateScript\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideEvaluatorScopedAPI.md).\n\n## GlideScopedEvaluator - getVariable\\(String name\\)\n\nReturns a variable from a GlideScopedEvaluator object.\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|The name of the variable.|\n\n|Type|Description|\n|----|-----------|\n|Object|The value of the specified variable.|\n\n```\n//setting up a record that contains the script to be executed.\nvar now_GR = new GlideRecord('global'); \nnow_GR.u_short_description = 'Calculate Addition';  \nnow_GR.u_test_script = \"result = x + y\"; \nevaluator.evaluateScript(now_GR, \"script\")\nnow_GR.insert(); \n \nvar evaluator = new GlideScopedEvaluator();\nevaluator.putVariable('x', 100);\nevaluator.putVariable('y', 200);\nevaluator.putVariable('result', null);\nevaluator.evaluateScript(now_GR, \"script\")\nnow_GR.insert();\n```\n\nOutput:\n\n```\n300\n```\n\n### Scoped equivalent\n\nTo use the getVariable\\(\\) method in a scoped application, use the corresponding scoped method: [getVariable\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideEvaluatorScopedAPI.md).\n\n## GlideScopedEvaluator - putVariable\\(String name, Object value\\)\n\nPuts 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|The name of the variable.|\n|value|Object|The value of the variable.|\n\n|Type|Description|\n|----|-----------|\n|void| |\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\";\nevaluator.evaluateScript(now_GR, \"script\") \nnow_GR.insert(); \n \nvar evaluator = new GlideScopedEvaluator();\nevaluator.putVariable('x', 100);\nevaluator.putVariable('y', 200);\nevaluator.putVariable('result', null);\nevaluator.evaluateScript(now_GR, \"script\")\nnow_GR.insert();\n```\n\nOutput:\n\n```\n300\n```\n\n### Scoped equivalent\n\nTo use the putVariable\\(\\) method in a scoped application, use the corresponding scoped method: [putVariable\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideEvaluatorScopedAPI.md).\n\n"}}