scheduled job randomly runs

El Cuchi
Tera Guru

Hi All,

 

i trust this email finds you well.

 

Situation

The business would like to automate notifications to specific groups when an outage related to a normal change  starts and ends. For this purpose i created 2 scheduled jobs. One for the start message and a second one for the end message. 

 

Issue

They do not run all the time and since they are not reliable we cannot use them.

 

Scheduled job for the start. It runs every second

In this job i look for outages linked to normal changes at the time of the start of the outage. If not approved a deferred event is inserted and email sent; and if approved another event is inserted which also triggers a notification. 

 

var todayDate = gs.nowDateTime();
var out = new GlideRecord('cmdb_ci_outage');
out.addEncodedQuery("task_numberSTARTSWITHchg^beginONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^task_number.ref_change_request.type=normal");
out.query();
while(out.next()) {

  var bdate = out.begin.getDisplayValue();
  var difference = gs.dateDiff(todayDate,bdate,false);
  var today = nowDateTime();

  if(today == bdate){
	if(out.task_number.state == '-2' || out.task_number.state == '-1') {
      gs.eventQueue('change.outage',out,out.sys_id,'now');
	} else if(out.task_number.state == '-5' || out.task_number.state == '-4' || out.task_number.state == '-3' || out.task_number.state == '4') {
			gs.eventQueue('change.outage',out,out.sys_id,'def');
		}
	}
}

  

Scheduled Job for the End.

In this job i look for outages linked to normal changes at the time of the end of the outage in scheduled or implemented state . If found, an event is inserted and email sent.

 

var todayDate = gs.nowDateTime();
var out = new GlideRecord('cmdb_ci_outage');
out.addEncodedQuery("task_numberSTARTSWITHchg^endONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^task_number.stateIN-2,-1^task_number.ref_change_request.type=normal");
out.query();
while(out.next()) {
  var today = nowDateTime();
  var edate = out.end.getDisplayValue();
  var difference = gs.dateDiff(todayDate,edate,false);

  if(edate == today) {
      gs.eventQueue('change.outage',out,out.sys_id,'end');
  }

}

 

Will it be possible that because by the time the evaluation finishes time is different and the event not recorded?

 

your help is appreciated.

5 REPLIES 5

See here:

 

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server_legacy/c_GlideSystemAPI#...

 

on scheduling your event. The script logic in a BR can schedule the event as you desire it seems.