Exception when calling script include from a different script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2020 01:32 PM
Hi,
encountering a sporadic error when trying to call a different script include.
var mySI = "MyOtherSciptInclude";
var k = new global[mySI]();
In my testing, the above code will work 90% of the time. When it does fail, my try/catch reports: undefined is not a function
Any insight/suggestions would be appreciated.
Patrick
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2020 01:39 PM
Hi, normally I would invoke a script include/function call with something like
var mySI = new MyOtherSciptInclude().myFunction();
// or
var mySI = new MyOtherSciptInclude();
var k =mySI.myFunction();
//Alternate for global scope
var mySI = new global.MyOtherSciptInclude().myFunction();
// or
var mySI = new global.MyOtherSciptInclude();
var k =mySI.myFunction();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2020 01:39 PM
Are you reffering to different scope when you say different script include?
If yes, then the syntax would be something like this.
var SI = new global.script_include();
SI.function_name();
For different scopes we use API name of script include instead of Script include name.
Thanks,
Sharjeel
Muhammad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2020 01:56 PM
Thanks for the quick responses 🙂
Both script includes are in 'Global' and the reason for my code formatting is that I have my code in a for loop and the value of 'mySI' is different for each iteration. ie
for( mySI in this.myObj) {
// if i write mySI to logs here, it is correct, but can fail on next line
gs.log(mySI); // correct value
var k = new global[mySI]();
k.someFunction(parm1...);
}
Thanks
Patrick

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2020 04:48 PM
Hi Patrick,
What is the type of value in this.myObj i.e. function, string, object?
you can check the type of this.myObj by using typeof() function. like this
gs.info(typeof(this.myObj));
To call ScriptInclude with dynamic name on each iteration the name should be of type function. If it is already of type function update the line as below.
//var k = new global[mySI]();
var k = new this.myObj[mySI]();
If the type of this.myObj is a string then you will need to first convert it to type function. eval() is used to convert string to function but please mind eval() is never recommended and has high-security concerns.
var str = eval(this.myObj[mySI]);
Let me know how it goes. If you still face any issue do let me know the type of value in this.myObj so that I can further help you on this.
Thanks & Regards,
Sharjeel
Muhammad