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

Scheduled jobs don't have a 'current' object. You're sending a null object to the notification.

If you want to pass a record (or series of records and series of events), you'll have to do a GlideRecord lookup. In which case, I would move the Owner Group is not empty check to the GlideRecord lookup in the scheduled job. Something like this:

var cGr = new GlideRecord('sc_cat_item');
cGr.addNullQuery('u_owner_group');
cGr.query();

while (cGr.next()) {
   gs.eventQueue('catalogitem.noti', cGr, '', '');
}

Note, code is untested and may require modifications to work on your system.

FYI - A response to this post was featured on the Community Live Stream video. I invite you to watch the episode.

Video: Community Live Stream - 2020-01-23 - Developer Community - ServiceNow Community

Thanks very much Chuck.

Hi Chuck;

 

Thanks for this above script which will help to create scheduled jobs through BR. My requirement to schedule a job for a later time needs to trigger a notification per the follow-on time on the incident record. I wrote below in BR. But "gs.eventQueue" not passing the actual value of "current.assigned_to" to the scheduled job. Due to this it failed to send notification due to not found assigned to. Can you please help on this, 

 

 

-----------------BR------------

var job = new GlideRecord('sysauto_script');
job.newRecord();
job.name = current.number;
job.active = true;
job.run_type = 'once';
job.run_start = current.follow_up;
job.script = 'gs.eventQueue(follow_up_time,current,current.assigned_to);';
job.insert();

 

-----------------------

bernyalvarado
Mega Sage

Hi Kiki,



Out of the box there's a script include BillingReportUtil which has a function to create a scheduled job on the fly. It's similar to what Chuck shared but I'm adding it just for reference in case it helps and also because it's a production "tested" version i guess :



the function is called from a business rule on the following way:



var billingUtil = new BillingReportUtil();


billingUtil.createScheduledJob(current, 'sysauto_script', 'Amazon', 'AwsBillingReportUtil');




The method createScheduledJob within the BillingReportUtil script include is the following:



      // Used for report schedule business rule to generate/update scheduler


      createScheduledJob : function(currentGr, schedulerTable, provider, scriptName) {


              var scheduleJobId = currentGr.scheduler;


              var gr = new GlideRecord(schedulerTable);


              var name = provider + ' Billing Report Scheduler Job for : ' + currentGr.name;


             


              if (currentGr.scheduler.nil()){


                      gr.name = name;


                      gr.active = true;


                     


                      var script = 'var billingUtil = new BillingReportUtil(); \n';


                      script = script + 'if(!billingUtil.isBillingWorkflowExecuting(\''+currentGr.sys_id+'\'));\n';


                      script = script + '       billingUtil.runThisJob(\''+currentGr.sys_id+'\');';


                      script = script.replace('BillingReportUtil', scriptName);


                      gr.setValue('script',script);


                     


                      var sysId = gr.insert();


                      currentGr.setValue('scheduler',sysId);


                      if(provider == 'Azure')


                              currentGr.update();


              }


              else{


                      gr.get(currentGr.scheduler);


                      gr.name = name;


                      gr.update();


              }


      }



Thanks,


Berny