
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 09-14-2018 12:06 AM
I just want to share this article that might be helpful,I faced similar issue 1 year back and recently my colleagues faced as well.
Can we make object creation and function execution dynamic for script includes in Servicenow ?
The answer is YES, Servicenow uses RHINO engine to execute all the javascript code that we write in the platform.So it supports all the javascript patterns to create object(based on RHINO version).
There are few possible
1. Using eval() or GlideEvaluator.evaluateString
2. I will provide the other way
Ideally when we are trying to use script includes we create its object and use its functions, as it is object all its methods(functions defined) are treated as name value pairs i.e for the below sample script include,
var testInclude = Class.create();
testInclude.prototype = {
initialize: function() {
},
method1:function(){
return 'method1';
},
method2:function(){
return 'method2';
},
type: 'testInclude'
};
if i execute below code it returns all the keys in that object.
var obj = new testInclude();
for(var i in obj)
gs.print(i);
i.e.
*** Script: initialize
*** Script: method1
*** Script: method2
*** Script: type
Now we can use same pattern to invoke it dynamically.How ?
create a object as shown in below snippet(you can use name value pair data type or any property or string fields in SNOW tables)
Refer below method to execute a script include dynamically, as we can access keys of created object as described above we can use the same way to invoke those functions as well. Using this method we can use script includes and its functions dynamically. We leverage more by using Arguments.
var obj={
'include':testInclude,
'include2':testInclude2,
'callFun':'method1',
'callFun2':'method2'
}; //define object in this manner to store all our script include and function mapping
var newObj = new obj['include'](); // this is same as var newObj =new testInclude()
var result =newObj['method1']();
or
var result=newObj[obj.callFun]();
*** Script: method1 //on executing above code it will print like this
Hope this helps 🙂
- 1,922 Views

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Great article @Naveen Velagapu, it was very helpful. 👏