Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Auto Close Incident based on business hours

Dazler
Mega Sage

Hi,

I was wondering if there is a way to auto close an incident after it has been resolved based on business hours and not business days.

I have a business rules but it looks at that sys property for auto close.  Our day hours are 11 hours a day (Mon - Thursday), except Friday which is 10 hours.  I need to set the incident to auto close based on 24 business hours.  

How can I achieve this without using the glide.ui.autoclose.time property?

Any help would be appreciated.

1 ACCEPTED SOLUTION

DrewW
Mega Sage

So I did a post on this before but I cannot seam to find it.  You can set this as a scheduled job or just update the Incident Autoclose BR I think it is.  There are a number of ways to do this but I find the below the fastest and it allows you to be flexible since you just have to make sure the schedule is up-to-date with business hours, weekends and holidays.

var hours = 24;
//Setup a schedule that has your business hours and put the sys_id on the next line.
var sch = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828")
var startDT = new GlideDateTime();
var endDT = new GlideDateTime();
//Get a starting point in the past
startDT.addSeconds(hours * 60 * 60 * -1); //hours in the past.
var loopCount = 0;
var found = false
//Keep going until we find what we need or loop count hits 100 so we do not loop infinatly.
while(!found && loopCount < 100){
    var dur = sch.duration(startDT, endDT).getNumericValue() / 1000 / 60 / 60;  //Convert to hours
    if(dur >= hours){
        //We found the first start time that is the correct number of hours in the past.
        found = true;
    } else {
        //If we did not find it then subtract 1 hour and loop back around.
        startDT.addSeconds(60*60*-1); //subtract 1 hour
    }
    loopCount++;
}
//Update all of the resolved incidents that have a resolved date on or before the start date we found.
var gr = new GlideRecord('incident');
gr.addQuery("state", "6").addOrCondition("incident_state", "6"); //These two should always be ins sync so you should not have to do this.
gr.addQuery("resolved_at", "<=", startDT);
gr.setValue("state", 7);
gr.setValue("close_notes", "Auto closed after " + hours + ".");
gr.updateMultiple();

View solution in original post

3 REPLIES 3

DrewW
Mega Sage

So I did a post on this before but I cannot seam to find it.  You can set this as a scheduled job or just update the Incident Autoclose BR I think it is.  There are a number of ways to do this but I find the below the fastest and it allows you to be flexible since you just have to make sure the schedule is up-to-date with business hours, weekends and holidays.

var hours = 24;
//Setup a schedule that has your business hours and put the sys_id on the next line.
var sch = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828")
var startDT = new GlideDateTime();
var endDT = new GlideDateTime();
//Get a starting point in the past
startDT.addSeconds(hours * 60 * 60 * -1); //hours in the past.
var loopCount = 0;
var found = false
//Keep going until we find what we need or loop count hits 100 so we do not loop infinatly.
while(!found && loopCount < 100){
    var dur = sch.duration(startDT, endDT).getNumericValue() / 1000 / 60 / 60;  //Convert to hours
    if(dur >= hours){
        //We found the first start time that is the correct number of hours in the past.
        found = true;
    } else {
        //If we did not find it then subtract 1 hour and loop back around.
        startDT.addSeconds(60*60*-1); //subtract 1 hour
    }
    loopCount++;
}
//Update all of the resolved incidents that have a resolved date on or before the start date we found.
var gr = new GlideRecord('incident');
gr.addQuery("state", "6").addOrCondition("incident_state", "6"); //These two should always be ins sync so you should not have to do this.
gr.addQuery("resolved_at", "<=", startDT);
gr.setValue("state", 7);
gr.setValue("close_notes", "Auto closed after " + hours + ".");
gr.updateMultiple();

Hi Drew,

Thank you for this.  It is very helpful.  I did have one question, the startDT put the time at 3 hour ahead but not in what the system timezone is set.  How can include that in this script?

You can set a time zone when you create the schedule object, check the dev docs. The system does everything in GMT so what makes you say it put it at 3 hours ahead?  How did you print out the value?  Did you make a change to the script to accommodate any changes you needed?

When I test it as a background script I get the expected date/time based on when I run it. Example if I print out the display values for startDT and endDT I get

*** Script: 03-04-2022 08:02:02
*** Script: 03-08-2022 14:02:02

Since my schedule is 9 hours a day excluding weekends and I ran it at 14:02:02 I have ~6 hours for the rest of today and then 9 for Monday and 9 for Friday so it did find the proper date/time.