How do I create a schedule job when a business rule is triggered?

kiki330611
Kilo Expert

Hi Everyone,

We have a requirement from the client that whenever a new record is inserted into a table, the business rule would run to create a schedule job that would only run once at the set time.

Basically every record in this table would contain a time field.

ex:                             Name                   Time

Record 1.           Amy             12/5/2016 12:00

Record 2.           Jean             22/5/2016 08:00

Record 3.           James           20/5/2016 07:00

Whenever someone create a new record, a new schedule job should be created, set to run script, run only once and start at the specific time we enter for that record.

So if we create the three records above, three schedule jobs should be created and run once at the time specified in the time field above.

I am very new to scripting in ServiceNow so just wondering if there is any script that is for create new schedule job and set parameters as well?

Thank you!

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Kiki,



Yes, this can be done. What sort of scheduled job are you looking for? A report? A script? You can run an AFTER business rule to insert a new record in the sysauto_script table (for a script.) If you want to run some other job, let me know.



Something like this (untested)


Name: Create new scheduled job


When: After


Table: (your table full of records)


Insert: true


Update: false


Advanced: true


Condition: (empty)


Script:


function onAfter(current, previous) {


    var s = new GlideRecord('sysauto_script');


        s.newRecord();


        s.name = current.name;


        s.active = true;


        s.run_type = 'once';


        s.run_start =       current.time;


        s.script = '// Insert your Javascript here';


}


View solution in original post

16 REPLIES 16

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Kiki,



You mean for each new record update you want to create a scheduled job?


Not sure what is the business reason for this requirement. Can you please elaborate further so that we can come up with better solution.



Meanwhile you can refer below link for more info on Server side code and Scheduled job.


http://wiki.servicenow.com/index.php?title=Business_Rules


http://wiki.servicenow.com/?title=GlideRecord#gsc.tab=0


Creating a Scheduled Job - ServiceNow Wiki


Chuck Tomasi
Tera Patron

Hi Kiki,



Yes, this can be done. What sort of scheduled job are you looking for? A report? A script? You can run an AFTER business rule to insert a new record in the sysauto_script table (for a script.) If you want to run some other job, let me know.



Something like this (untested)


Name: Create new scheduled job


When: After


Table: (your table full of records)


Insert: true


Update: false


Advanced: true


Condition: (empty)


Script:


function onAfter(current, previous) {


    var s = new GlideRecord('sysauto_script');


        s.newRecord();


        s.name = current.name;


        s.active = true;


        s.run_type = 'once';


        s.run_start =       current.time;


        s.script = '// Insert your Javascript here';


}


Thanks ctomasi!! I wrote the script like you suggested and it worked! Thanks for your help!!


Hi Chuck,



Any idea if this is still possible in Jakarta?


I had a business rule running against the sys_trigger table, no luck.


Then ran a business rule against the sysauto_script table, no luck.


Then I found this thread and amended my code to match your suggestion, no luck.



Table: Incident [incident]


When: after


Insert: True


Update: True


Condition:


On hold until > Changes (this is my custom field)



(function executeRule(current, previous /*null when async*/) {



        var sys_id = current.sys_id;


        gs.addInfoMessage('sys_id is ' + sys_id);


        var num = current.number;


        gs.addInfoMessage('number is ' + num);


        var ohu = current.u_on_hold_until;


        gs.addInfoMessage('on hold until is ' + ohu);



        var n_trigger = new GlideRecord('sysauto_script');


        n_trigger.newRecord();   //also tried with "initialize()" and without "initialize()" or "newRecord()"


        n_trigger.name = sys_id;


        n_trigger.active = true;


        n_trigger.run_type = once;


        n_trigger.run_start = ohu;


        n_trigger.script = 'gs.log("*****Running from auto job*****");';


        n_trigger.insert(); //also tries without (as per your "newrecord()" method) and with an "update()" instead



})(current, previous);





Do you know if this is still possible, or has the ability to create an entry on this table via server side script been restricted?


Alternatively can you see any glaringly (embarrassingly) obvious mistakes in the above?