Problem with calling function inside the same script include

Serhii5
Mega Guru

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?

1 ACCEPTED SOLUTION

bernyalvarado
Mega Sage

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

View solution in original post

5 REPLIES 5

bernyalvarado
Mega Sage

Hi @Serhii @Serhii,

 

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 

bernyalvarado
Mega Sage

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

Yes, thanks a lot for your answer, I decided my problem, it was inside script include

Cool! You're welcome! 🙂