- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2021 01:21 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2021 04:22 AM
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:
Reference:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2022 01:37 AM
var gr - some GlideRecord
var script - some code
var param = new Packages.java.util.HashMap();
param.put("current", gr);
GlideEvaluator.evaluateStringWithGlobals(script, param);