As I've answered in another question, the function is often written in Script Include instead of with the script creating ScheduleOnce).

So something like below. 

Script Include:

var ScheduledJobExample = Class.create();
ScheduledJobExample.prototype = {
    initialize: function() {},
    output2log: function(argument) {
        gs.info(argument);
    },
    type: 'ScheduledJobExample'
};

Schedule script:

var argument ="Test OK";
var sched = new ScheduleOnce();
sched.script = "var si = new ScheduledJobExample().output2log('" + argument + "');";
sched.schedule();

Here, sched.script is create a script include class ScheduledJobExample and calls function named "output2log()" with argument defined by variable "argument".

That is, sched.script is just a string with javascript script to execute in the job.

thank you for answering.
The code for "Schedule Once" is as follows:

   schedule: function() {
      var t = this._getTrigger();
      
      // calc and set time
      var n = parseInt(this.seconds);
      n += parseInt(this.minutes) * 60;
      n += parseInt(this.hours) * 60 * 60;
      n += parseInt(this.days) * 24 * 60 * 60;
      this.time.addSeconds(n);
      t.next_action.setValue( this.time );
      
      t.trigger_type = this.trigger_type;
      gs.print("Scheduling: " + this.label + " for: " + t.next_action.getDisplayValue());
      return t.insert();
   },

 

 

var t = this._getTrigger();

In the above code, it seems that the following code of the contents of "Schedule" of the script include is called.

  // call after variables are setup
  _getTrigger: function() {
     var t = new GlideRecord('sys_trigger');
     t.initialize();
     t.name = this.label; 
     t.document = this.document;
     t.document_key = this.document_key;
     t.script = this.script;
     t.job_id = this.job_id;
     if (!gs.nil(this.system_id))
       t.system_id = this.system_id;
     return t;
  },

 

I found that the above code created a new record in the "sys_trigger" table.

Does "sched.script =" when using "Schedule Once" mean to set the script in the yellow marker part of the image below?

 

find_real_file.png

 

Do you know where to check the results of the schedule you ran?

Yes.

find_real_file.png

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

find_real_file.png

View solution in original post

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.