Auto Close tasks one week after task is triggered consider business days-scheduled job

Gayatrin
Tera Contributor

Hi all,

I have a requirement that  if a task is active & if Assigned to is empty, I need to close the task mark as "Closed Complete" one week after task is triggered consider Business Days (Monday to Friday between 9AM and 5PM) . Please guide. Here is the scheduled job..

Gayatrin_0-1692452225731.png

 

 

5 REPLIES 5

Eswar Chappa
Mega Sage
Mega Sage

Hi @Gayatrin, the below community solution will help you

 

https://www.servicenow.com/community/developer-articles/scheduled-job-to-calculate-business-duration...

Cheers, hope that helps

Eswar Chappa

*** Please mark as "Correct" or "Helpful" as appropriate ***

 

Hi @Gayatrin Please find the below evidence for the Incident for the similar funcationality which was evaluation 7 Business days

 

EswarChappa_0-1692511116114.png

var htask = new GlideRecord('sn_hr_core_task');
htask.addEncodedQuery('active=true^assigned_toISEMPTY');
htask.query();
while(htask.next()){
var startDateTime = htask.sys_created_on;
var endDateTime = new GlideDateTime(); 
var dur = new DurationCalculator();
// Set 9-5 weekday schedule. This is the schedule in which endDateTime, seconds, and totalseconds is set
dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); //Provide the sys_id of the schedule you use for business days calculation here
dur.calcScheduleDuration(startDateTime, endDateTime);
var businesdays = dur.getSeconds()/(60*60*9);//coverting Business duration time which was in seconds in to days
if(businesdays>7){
htask.setValue('state', '3');//please update the state vlaue of closed here
htask.update();
}
}

Cheers, hope that helps

Eswar Chappa

*** Please mark as "Correct" or "Helpful" as appropriate ***

 

AnveshKumar M
Tera Sage
Tera Sage

Hi @Gayatrin 

You can try replacing your code from line 10 with the following code.

 

var tskGr = new GlideRecord('sn_hr_core_task');

taskGr.addQuery('active', true);

taskGr.addQuery('assigned_to', '');

taskGr.query();

while(taskGr.next()) {

 var createdGdt = new GlideDateTime(gr.sys_created_on.getDisplayValue());

 var todayGdt = new GlideDateTime();

 var busDur = sched.duration(createdGdt, todayGdt);

 var busDurDays = busDur.getDayPart();

  

  if(busDurDays > 7){

    taskGr.setValue('state', '3');

    taskGr.update();

  }

}

 

Thanks,
Anvesh

Hi @AnveshKumar M 

I have tried with this, but it is not running I'm trying to print busDurDays, it's empty.

Gayatrin_0-1692702890825.png