Incidents are set to closed after 48 hours of being moved to 'Resolved state excluding weekends

Nagashree5
Tera Contributor

Hi All,

 

We have a custom table that is called "IT Incidents". After being resolved, these IT Incidents needs to be set to closed after 48 hours of being resolved and weekends should be excluded.

Can anyone please suggest.

 

TIA.

8 REPLIES 8

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Nagashree5 ,

 

Check this link for solution. Please modify the days/time as per requirement.

 

https://www.servicenow.com/community/itsm-forum/auto-closure-of-task-after-3-business-days/m-p/48954...

 

Thanks,

Danish

 

SANDEEP28
Mega Sage

@Nagashree5 First you need to create a schedule record where you can exclude weekends and use in below script.

I have created script for incident table. You can modify for your table. You would need to create schedule job and put this script into it then schedule it for daily to run

 

 

var currentDateTime = new GlideDateTime();
var grIncident = new GlideRecord("incident");
grIncident.query('state', '6'); //get the resolved incidents
grIncident.query();
while(grIncident.next()) {
var dur = new DurationCalculator();
dur.setSchedule('090eecae0a0a0b260077e1dfa71da828');   // put the sys_id of schedule
dur.calcScheduleDuration(grIncident.resolved_at, currentDateTime);
var totalSecs = dur.getTotalSeconds();

d = Number(totalSecs);
var hours = Math.floor(d / 3600);
if (hours >= 48)  // if total hours are more than 48
{
grIncident.setValue('state', '7'); //close the incident
grIncident.update();
}
}

 

 

Schedule -

SANDEEP28_0-1701860140593.png

 

 

SANDEEP28_1-1701860182210.png

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

 

 

 

Hi @SANDEEP28,

 

Thanks so much for your response.

 

I have written the scheduled job with the provided code and created a schedule as well like below.

var currentDateTime = new GlideDateTime();
var grIncident = new GlideRecord("it_case");
grIncident.query('state', '150'); //get the resolved incidents
grIncident.query();
while(grIncident.next()) {
var dur = new DurationCalculator();
dur.setSchedule('c943f56187a7719056ed2f8f8bbb355c');   // put the sys_id of schedule
dur.calcScheduleDuration(grIncident.u_resolved_at, currentDateTime);
var totalSecs = dur.getTotalSeconds();

d = Number(totalSecs);
var hours = Math.floor(d / 3600);
gs.info("the hours are " + hours);
gs.info("the d are " + d);
if (hours >= 48)  // if total hours are more than 48
{
grIncident.setValue('state', '7'); //close the incident
grIncident.update();
}
}

Nagashree5_0-1704633522987.png

Nagashree5_1-1704633553383.png

But, it is working for all the days, the hours are getting counted for weekends also, Can you please guide me on this.

 

Thanks in Advance.

SANDEEP28
Mega Sage

@Nagashree5 If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!