
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2020 08:37 AM
Hi guys, I have a problem with calling a function in the same script include,
slicedata: function(field) {
var table_created_on = new GlideDateTime(field);
var table_date = table_created_on.getDate();
var table_month = table_date.toString().split("-")[1];
return table_month;
}
and after that im trying to call this function inside another function but same script include
assigneduser: function(table, current) {
var tickets = new GlideRecord(table);
tickets.addQuery('assigned_to', current.u_user);
tickets.query();
var tickets_count = 0;
while (tickets.next()) {
var ticket = this.slicedata(table.sys_created_on).slicedMonth;
}
problem is - when i call function without underscore - its work, without - none.
var ticket = this._slicedata(table.sys_created_on).slicedMonth; - that's work
Could I ask you're to explain to me, why that happens?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2020 04:12 PM
Here goes some examples that should be able to demonstrate what I'm explaining:
Using a script include with the name testSI:
var testSI = Class.create();
testSI.prototype = {
initialize: function() {
},
sayHello: function(){
gs.addInfoMessage('Hello');
this._sayIntro();
this.sayGoodbye();
},
_sayIntro: function(){
gs.addInfoMessage('I hope you are doing great');
},
sayGoodbye: function(){
gs.addInfoMessage('Good bye');
},
type: 'testSI'
};
If externally I call the following
var testGreetings = new testSI();
testGreetings.sayHello();
the result will be
Background message, type:info, message: Hello
Background message, type:info, message: I hope you are doing great
Background message, type:info, message: Good bye
Both _sayIntro (with the underscore) and sayGoodbye are successfully called from the sayHello method
We can even call the internal method with the underscore from outside the script include... although, as previously said... if it has an underscore it's because it's not design to be called from outside but rather from within the script include, yet, it's call will be successful.
var testGreetings = new testSI();
testGreetings._sayIntro();
Background message, type:info, message: I hope you are doing great
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2020 04:03 PM
Hi
I hope you're doing great!
Within a method of a script include you can call another one regardless if it starts with an underscore (_) or not. Using the underscore as a prefix of a method is just a best practice to have a visual aid of which methods are intended for internal use within your script include. It's not the same as using private in a class or struct in other programming languages because you can still call the method with underscore outside of the script include.
I hope this helps! 🙂
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2020 04:12 PM
Here goes some examples that should be able to demonstrate what I'm explaining:
Using a script include with the name testSI:
var testSI = Class.create();
testSI.prototype = {
initialize: function() {
},
sayHello: function(){
gs.addInfoMessage('Hello');
this._sayIntro();
this.sayGoodbye();
},
_sayIntro: function(){
gs.addInfoMessage('I hope you are doing great');
},
sayGoodbye: function(){
gs.addInfoMessage('Good bye');
},
type: 'testSI'
};
If externally I call the following
var testGreetings = new testSI();
testGreetings.sayHello();
the result will be
Background message, type:info, message: Hello
Background message, type:info, message: I hope you are doing great
Background message, type:info, message: Good bye
Both _sayIntro (with the underscore) and sayGoodbye are successfully called from the sayHello method
We can even call the internal method with the underscore from outside the script include... although, as previously said... if it has an underscore it's because it's not design to be called from outside but rather from within the script include, yet, it's call will be successful.
var testGreetings = new testSI();
testGreetings._sayIntro();
Background message, type:info, message: I hope you are doing great

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2020 04:18 AM
Yes, thanks a lot for your answer, I decided my problem, it was inside script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2020 12:49 PM
Cool! You're welcome! 🙂