The CreatorCon Call for Content is officially open! Get started here.

Amit Gujarathi
Giga Sage
Giga Sage

Hi All,

Hope you all are doing fine.

 

  • As most of you must be aware of the script field which you must have seen whenever you create a new Business rule , client script , transform script and so on..
  • And it looks something as given below :
AmitGujarathi_0-1667562645004.png

 

  •  But do you know we can crate a custom Script field also , its one of the OOB data type for field which we can use the script type of field on any table and bring it to any form .
  • You can create script field on any table the way you create any other field just you have to provide type == script as shown in the image below :
 

GlideScopeEvaluator API :

  • The GlideScopedEvaluator API allows you to evaluate scripts in a GlideRecord field from both scoped and global server scripts.
  • The GlideScopedEvaluator API evaluates scripts within the script field type from the provided Glide Record. 
  • evaluateScript(GlideRecord grObj, String scriptField, Object variables)

    • Evaluates a script from a GlideRecord field.
    • 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.
    • Return :
      Type Description
      Object The result of the script execution.

 

Use Case :

  • In our case we have a workflow which is generic to around 50+ auto task integration.
  • Now each auto task we can consider as a separate integration tool will have the source table common but the payload will be different.
  • Now in order to have it as generic solution we have to change the payload or we can call it as script execution on the Run time based on the type of Auto task we are dealing with.

 

Solution :

  • So what we did is to create a custom table lets called it as Auto Task Configuration and it will store the different payload generation script based on the provided Auto task.
  • we don't have to change anything on the workflow we will simply passed the current object and based on current object task type it will provide the relevant script and we execute that script from the script field in  the Run script activity .
autoscripy.png

 

Script Include Functions :

  • Get the Script to be executed based on configuration
// Get configuration method to get the configuration (Script) from Auto Task configuration table
	getConfiguration: function(autoTableName) {
		var configRecord = new GlideRecord(<Auto Task Configuration Table>);
		configRecord.addQuery('u_active', true);
		configRecord.addQuery('u_table', autoTableName);
		configRecord.query();
		if (configRecord.next()) {
			return configRecord;
		} else {
			return '';
		}
	},
  •  Execute the Derived Script :
//Execute the Derived script using GlideScopedEvaluator API
execute: function() {
		
		var configRecord = this.getConfiguration(this.recordClass);

		if (configRecord != '') {
			var processingScriptResult = (function (autoTaskObj) {
				var scriptParams = {
					'autoTaskObj': autoTaskObj
				};
				
				var evaluator = new GlideScopedEvaluator();
				return evaluator.evaluateScript(configRecord, 'u_processing_script', scriptParams);
			})(this.autoTaskObj);
			
			return processingScriptResult;
		} else {
			gs.error('Configuration record not found for table: ' + this.tableName);
			return 'Configuration record not found for table: ' + this.tableName;
		}
		
	},

 

  • Run Script in Workflow :
var result = new ScriptIncludeName(current).execute();

 

Please be sure to bookmark this article as well as mark it as Helpful if you thought it was helpful.

 

Regards,

Amit Gujarathi

Technomonk Youtube 

Amit Gujarathi Linkedin 

TheTechnomonk.com 

ServiceNow Community Amit Gujarathi 

GitHub Amit Gujarathi

Comments
CMDB Whisperer
Mega Sage

This is a very powerful tool which I have used to create some very powerful scoped applications!  An important thing to keep in mind, however, is the implication of how Cross-Scope Applications work.  If you are developing a scoped app and you use GlideScopedEvaluator to run some script stored in an attribute, the question you should ask yourself is: do I know in advance what that script will contain?  If the script contains code that will attempt to access a Table, API, or execute a command that was not tested during design time, then ServiceNow may not be able to evaluate that code successfully!  So, think carefully of why you are using this method, and whether you can anticipate the kinds of script and types of access that will be required at runtime, and then preemptively include the cross-scope privileges that you will need for your application users to run the code successfully.  Once you deploy that application to another instance using the app repository (or via the ServiceNow Store) the application will not be able to create new cross-scope privileges on demand and your application will not work as expected!

Version history
Last update:
‎08-30-2023 11:07 AM
Updated by:
Contributors