How to use "Schedule Once" Part 2

bonsai
Mega Sage

※This is the Paris version.

 

I learned the basic usage in the following thread.

How to use "Schedule Once" - Service Management - Question - ServiceNow Community

 

It seems that an error occurs when the function you want to schedule is calling another function as shown below.
How can I resolve this case?

 

function script2(argument2){
  gs.info(argument2);
}


function script(argument){
    //ScheduleJob
    gs.info(argument);
    script2(argument + "~part2");
} 


var argument ="Test OK";
var sched = new ScheduleOnce();
sched.script = '('+script+')("'+argument+'");';
sched.schedule();
1 ACCEPTED SOLUTION

palanikumar
Mega Sage

Hi,

Scheduler does not know the script declared outside. You need to either define the second function inside the first function or create it in script include and refer it.

Refer sample code for first option:

function script(argument){
    //ScheduleJob
    gs.info(argument);
    script2(argument + "~part2");

    function script2(argument2){
        gs.info(argument2);
    }
} 


var argument ="Test OK";
var sched = new ScheduleOnce();
sched.script = '('+script+')("'+argument+'");';
sched.schedule();
Thank you,
Palani

View solution in original post

3 REPLIES 3

palanikumar
Mega Sage

Hi,

Scheduler does not know the script declared outside. You need to either define the second function inside the first function or create it in script include and refer it.

Refer sample code for first option:

function script(argument){
    //ScheduleJob
    gs.info(argument);
    script2(argument + "~part2");

    function script2(argument2){
        gs.info(argument2);
    }
} 


var argument ="Test OK";
var sched = new ScheduleOnce();
sched.script = '('+script+')("'+argument+'");';
sched.schedule();
Thank you,
Palani

Did you define the function inside the function !!
I was shocked because I didn't know.
This seems to solve various problems.

Yes, I defined function inside a function. This is working fine in my instance as well.

Please note that those functions are not accessible outside this function. If you want your function to be called from multiple places then it is preferred to create it in Script Include.

Thank you,

Palani

Thank you,
Palani