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

Gurpreet07
Mega Sage

Hi Kiki,



Here is a suggestion   ... first look for the Scheduled jobs(you created earlier) which already been executed (runOnce)... if found any then update that one.In case you are not able to find out such record , create new sj. In this way you may able to reduce the number of schedule jobs.


damianj_
Tera Contributor

Hi Kiki,



Please consider using scheduled event(gs.eventQueueScheduled(...)) with script action. You did not describe what exactly you want to achieve but for one-time execution scheduled event seems like something that might work for you.



http://wiki.servicenow.com/index.php?title=Notification_Examples#Define_a_Scheduled_Event


http://wiki.servicenow.com/index.php?title=Script_Actions#gsc.tab=0



// Damian