How to use "Schedule Once" Part 3

bonsai
Mega Sage

※Paris version

※I'm sorry for being persistent.

 

I felt that I could get a rough idea of how to use "Schedule Once" and its features in the thread below.

https://community.servicenow.com/community?id=community_question&sys_id=bac79caedb490d14da1999ead396199b

https://community.servicenow.com/community?id=community_question&sys_id=013f0d66db45c190382a82630596198a

 

However, there is one part that I still don't understand.

What does "sched.script =" in the code below mean?

I can't figure out what I'm doing here because I can't find a function named "script" in "Schedule Once".

function script(argument){
    //ScheduleJob
    gs.info(argument);
} 
var argument ="Test OK";
var sched = new ScheduleOnce();
sched.script = '('+script+')("'+argument+'");';
sched.schedule();

 

1 ACCEPTED SOLUTION

Yes.

find_real_file.png

Execution result in "System Logs" > "System Log" > "All"

find_real_file.png

View solution in original post

8 REPLIES 8

Yes.

find_real_file.png

Execution result in "System Logs" > "System Log" > "All"

find_real_file.png

Thank you!
I deepened my understanding!

Hi Hitoshi, 

I am trying to schedule a function that belongs to the same class using your method, but It does not seem to work. 

 

var scheduleTest = Class.create();
scheduleTest .prototype = {
    initialize: function(addButton) {
		gs.debug("User in initialize funciton: " + gs.getUser());
		this.addButton = addButton
    },
	debugLog: function(){
		gs.debug("Schedule executed");
	},
	
	mySchedule: function(){
        gs.debug("scheduleTest reached");
		var argument ="Schedule test";
		var gdt = new GlideDateTime();
		gdt.addSeconds(1);
		var sched = new global.ScheduleOnce();
		sched.script = '('+debugLog+')("'+argument+'");';
		sched.setTime(gdt);
		sched.setLabel("run this as system");
		sched.schedule();
	},

    type: 'scheduleTest '
};

 

scheduleTest() is called from another script include. scheduleTest() executes fine (I can see the debug message in Session Logs) but it does not call schedTest(). I am not sure what I'm doing wrong here. Any help would be appreciated. 

 

Thanks, 

T.

Hi Hitoshi, 

 

I am trying to schedule a function using your method, but it doesn't work for me. Could you help me and tell me what I am doing wrong in my code? runSchedule function is called by another script - I know the call is successful because I can see the debug message in Session Log.

 

var scheduleTest = Class.create();
scheduleTest.prototype = {
    initialize: function(addButton) {
		gs.debug("User in initialize function: " + gs.getUser());
    },
	
	testFunction: function(){
		gs.debug("Test function executed");
	},
	
	runSchedule: function(){
		
		var argument ="Schedule executed";
		var sched = new global.ScheduleOnce();
		sched.script = '('+testFunction+')("'+argument+'");';
        gs.debug("Schedule reached");
		sched.schedule();
	},

    type: 'scheduleTest'
};

Thanks in advance, 

T.