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

Bert_c1
Kilo Patron

You may want to change your approach and use a Business Rule defined on the 'cmdb_ci_outage' table. And check the 'begin' and 'end' values of records. Use Filter conditions on 'When to run', and check 'Insert' and 'Update' there.

 

A scheduled job that runs once a second is a bad idea. Will cause performance problems, as you have experienced. A BR only runs when a record is updated.

Hi Bert,

 

thank you so much for replying.

I dont really understand your logic. when will the BR run? when the start or end dates come the outage record is not created nor updated, so the BR wont be triggered.

 

regards,

Max

Hi El Cuchi,

 

See:

https://<instance>.service-now.com/sys_script_list.do?sysparm_query=GOTOcollection%3Dcmdb_ci_outage&...

 

One example:

Screenshot 2024-11-01 103625.png

Uncheck 'Delete', add Filter conditions for the two fields. And put your script logic to create events in the Advanced tab:

Screenshot 2024-11-01 103747.png

 

And read about business rules here:

 

https://docs.servicenow.com/bundle/xanadu-api-reference/page/script/business-rules/concept/c_Busines...

 

Use Google if needed.

 

 

Hi Bert,

 

My understanding is that your proposed solution will create events when the outage is created/updated and that is not what i need.

I need the event to be created when the outage starts and ends.

 

regards,

Max