Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to use "Schedule Once"

bonsai
Mega Sage

※Paris version

I'm aiming for script concurrency using "Schedule Once".

I understand how to use it as follows,
I don't understand how it works.

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

 

I wondered if "script" should be a string in the "'(' + script +')" part, but why specify the function name?

I'm learning a script so please let me know.

1 ACCEPTED SOLUTION

Re-read the original question.

No, "script" should not be a string. It's a function.

For example, following code will call the function named "script" immediately.

function script(argument){
    //ScheduleJob
    gs.info(argument);
}
var argument ="Test OK"
(script)(argument);

As such, in sched.script, it's necessary to pass the function named "script" (i.e. the content of the function) and not the entire definition of the function including "function script(argument)) {".

sched.script = '('+script+')("'+argument+'");';

That is, 

sched.script = 'gs.info("test ok");';

and not this,

sched.script = 'function script(){gs.info("test ok");}';

View solution in original post

5 REPLIES 5

Hitoshi Ozawa
Giga Sage
Giga Sage

Working example of ScheduleOnce.

Following example will update user to be active.

Script Include:

var UpdateUserActiveFlg = Class.create();
UpdateUserActiveFlg.prototype = {
    initialize: function() {},
    updateUser2Active: function(argument) {
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', argument);
        gr.query();
        if (gr.next()) {
            gr.active = true;
            gr.update();
        }
    },
    type: 'UpdateUserActiveFlg'
};

Script using ScheduleOnce

var argument ="62826bf03710200044e0bfc8bcbe5df1";  // sample sys_id of user to set active
var sched = new ScheduleOnce();
sched.script = "var si = new UpdateUserActiveFlg().updateUser2Active('" + argument + "');";
sched.schedule();

BTW, the script in the question would return an error because it's missing a semi-colon after

var argument ="Test OK";

Re-read the original question.

No, "script" should not be a string. It's a function.

For example, following code will call the function named "script" immediately.

function script(argument){
    //ScheduleJob
    gs.info(argument);
}
var argument ="Test OK"
(script)(argument);

As such, in sched.script, it's necessary to pass the function named "script" (i.e. the content of the function) and not the entire definition of the function including "function script(argument)) {".

sched.script = '('+script+')("'+argument+'");';

That is, 

sched.script = 'gs.info("test ok");';

and not this,

sched.script = 'function script(){gs.info("test ok");}';

thank you for your answer.
And thank you for correcting the code mistake.

I learned that specifying a function name = passing the contents of a function! !!

 

By the way, I still don't understand why I need to pass the contents of the function. ..

Is it the specification of "Schedule Once"?