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

How to execute script include dynamically

Alon Grod
Tera Expert

Hi,

have the table u_merkaz_tom with the string field 'u_calculation'. This field holds inside a name of a script include and a function name, for example:
Median().isMedian()
How can I glide record this table and execute the script includes one by one according to the value inside 'u_calculation'.

I tried this way but its not working (im not getting the log):

 

Screenshot 2023-10-01 at 13.41.45.png

 

var reportsMerkazTom = Class.create();

reportsMerkazTom.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isTest: function() {

		var gr = new GlideRecord('u_merkaz_tom_reports');
		gr.query();
		while(gr.next()) {
			
			var part = gr.u_calculation.toString();
			var scriptIncludeName = part.split('.')[0];
			var funcName = part.split('.')[1];
			var scriptIncludeGR = new GlideScriptInclude(scriptIncludeName);
			scriptIncludeGR[funcName]();
			
		}
		
    },
    type: 'reportsMerkazTom'
});


var Median = Class.create();

Median.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isMedian: function() {


        gs.log('Mays');


    },
    type: 'Median'
});


 

1 ACCEPTED SOLUTION

jaheerhattiwale
Mega Sage

@Alon Grod Update your script include function script like below. This should work for you

 

isTest: function() {

        var gr = new GlideRecord('u_merkaz_tom_reports');
        gr.query();
        while(gr.next()) {
           
            var part = gr.u_calculation.toString();
            part = part.split('.');

            var scriptIncludeName = part[0].replace("()", "");
            var funcName = part[1].replace("()", "");

            var scriptIncludeGR = new global[scriptIncludeName]();
            scriptIncludeGR[funcName]();
        }
},
 
 
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

5 REPLIES 5

Alon Grod
Tera Expert

Sandeep Rajput
Tera Patron
Tera Patron

@Alon Grod Apart from the answer shared one of the previous thread (related to Calculated field), you can also choose to make u_calculation a script type field.

 

Screenshot 2023-10-01 at 4.47.54 PM.png

Screenshot 2023-10-01 at 4.49.26 PM.png

You can put your script inside this field. Now question comes that how can we evaluate this script, to evaluate a script available on a field ServiceNow has provided GlideScopedEvaluator API. 

Here is a sample how you can use this API.

 

var now_GR = new GlideRecord('global'); 
now_GR.u_short_description = 'Calculate Addition';  
now_GR.u_test_script = "result = x + y"; 
evaluator.evaluateScript(now_GR, "script")
now_GR.insert(); 
 
var evaluator = new GlideScopedEvaluator();
evaluator.putVariable('x', 100);
evaluator.putVariable('y', 200);
evaluator.putVariable('result', null);
evaluator.evaluateScript(now_GR, "script")
now_GR.insert();

For more information on GlideEvaluator please refer to the documentation https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/GlideEvaluatorAPI

 

Hope this helps.

 

@Sandeep Rajput hi, but if i want to do it in my way, using the scrips that I shared, how can I call the script include and the function dynamically? what do i need to change?

@Alon Grod Calculated field approach which I suggested in the previous thread is one of the best ways to calculate the values at the run time.