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

Hitoshi Ozawa
Giga Sage
Giga Sage

"script" is the name of the function that's defined at the top of the script.

function script(argument){
    //ScheduleJob
    gs.info(argument);
} 

It's really a bad name.  Can rename it as below:

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

var sched.script is set to the javascript command to execute. The script can also be written as below without declaring a function.

var argument ="Test OK";
var sched = new ScheduleOnce();
sched.script = 'gs.info("' + argument + '");';
sched.schedule();

Note that the output of the gs.info() is written to "System Log" > "Script Log Statement". The script is just outputting string "Test OK" to the log as can be seen below.

find_real_file.png

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?