We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to pass "current" dynamically within GlideEvaluator.evaluateString() as parameter..

Shariq Azim
Kilo Guru

Hello Everyone,

I am trying to run a script stored on a field/variable of a record(ritm)..Now I do not plan to create the "script"  type field on the form, thus ,cannot use GlideScopedEvaluator API (https://developer.servicenow.com/dev.do#!/reference/api/paris/server_legacy/GlideEvaluatorAPI#GlideEvaluator-evaluateScript_GR_S_O?navFilter=evaluatesc) .

 

I am able to manage with  GlideEvaluator.evaluateString() function , but unfortunately not able to use it properly.

For example, if the string field on the record has the value new script_include().function_name(current).  I am able  to call the correct function using

//example logic and code
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('sys_id', sys_id);// ihave this captured 
ritm.query();
 if (ritm.next()) {

var val="new script_include().function_name(current)";
var func_val;
var ct=func_str.indexOf("current");
if (ct >0){
	func_val= func_str.substring(0,ct)+
ritm+func_str.substring(ct+7) ;
}
	else{
func_val= func_str;
}

gs.print( GlideEvaluator.evaluateString(val) ); //new GlideScopedEvaluator().evaluateScript(gr, ' script_field_name',null))
// cannot be use as i dont have a script type field

}

the script include contains the code:

function: function_name(current){
  var reqstr_str;
  var ret_str = '';
  gs.log("script  starts  ="+current.number.toString() );
     
ret_str= current.number.toString() ;
        return ret_str;
}

So the intention is to get  fields like variables, number and etc.  The above code is  just an example not the complete logic.

Unfortunately, I am getting an error

Javascript compiler exception: missing ] after element list (<refname>; line 1) in:
new script_include().function_name([object GlideRecord])

I expect to get the answers as:

RITM0148336 // for example

But, obviously current is not passed into the script include as a gliderecord object but as   string if dont replace the current string within the parenthesis.

Any idea how to solve this?

 

Regards,

Shariq

 

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@Shariq 

see this example which is working fine

You need to pass the object with particular syntax

Script Include:

var getMyDetails = Class.create();
getMyDetails.prototype = {
	initialize: function() {
	},

	getMyNumber: function(obj) {
		gs.info('RITM number' + obj.number);
		return obj.number;
	},

	type: 'getMyDetails'
};

Calling it from scripts background:

var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('sys_id', '95cd4c0bdb9d6410224a2a9a48961933'); 
ritm.query();
if (ritm.next()) {
var gc = GlideController;
gc.putGlobal('ritm', ritm);
var val = "new getMyDetails().getMyNumber(ritm)";
var ret = GlideEvaluator.evaluateString(val);
gs.info(ret);
}

Output:

find_real_file.png

find_real_file.png

Reference:

https://community.servicenow.com/community?id=community_question&sys_id=2f684beddb1cdbc01dcaf3231f96...

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

kirillvo
Tera Contributor

var gr - some GlideRecord

var script - some code

var param = new Packages.java.util.HashMap();
param.put("current", gr);

GlideEvaluator.evaluateStringWithGlobals(script, param);