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.

Ravi Gaurav
Giga Sage
Giga Sage

Problem :-As of now , we can't close the MIs(major incident) automatically after been put to resolve by using OOTB  Auto Close BR, the major incidents will not be auto close in ServiceNow .We can close a major incident physically(manually ) in the wake of approving the goal and when the major incident is in the Resolved state. Explore to Major Incidents > Open. Open the significant occurrence that you need to close

Solution:- There is a possible solution for the same we can create a Schedule job that run every-day at a particular time and close the major-incident based on the condition.

Lets take an example below:-

We have one scenario where we have to close all the MIs in 45 day after being put to resolve.

So we can create an scheduled job where we will be writing the below code :-

In the below code we need to create one property where we will be defining our date i.e. after how many days the incident should close. In my case it is (glide.ui.autoclose.time.major') .

var ps = gs.getProperty('glide.ui.autoclose.time.major');

var pn = parseInt(ps);

var queryTime = new GlideDateTime();

queryTime.addDaysUTC(-pn);

    if (pn > 0) {

var gr = new GlideRecord('incident');

gr.addEncodedQuery('major_incident_state=accepted^state=6');// when the incident is in resolved state and accepted)

gr.addQuery('sys_updated_on', '<', gs.daysAgo(pn));

gr.query();

while(gr.next()) {

gs.print(gr.number);

        gr.state = 7; 

        gr.active = false;

        gr.closed_by = gr.resolved_by;

       gr.update();

}

}

In this way we can auto close the Major Incident.

Comments
shruti32
Giga Explorer

very helpful!

Hiranmayee Moha
Tera Expert

@Ravi Gaurav ....

My requirement is 'the auto closer process should be applied to both incidents & Major incidents' . So that both will auto close at the same time. 

 

Do I need to create a new sys property for MIs?

 

Thanks in Advance

KaareMarquardt
Tera Contributor

We had a similar request to close MIs after 24 hours. 
I created the following solution to acomplish this. 

Business rule

Name Major Incident Autoclose
When to run:

  • Insert
  • Update
  • State changes to Resolved
  • Major incident state is Accepted

Actions to take:

  • State to close

Advanced:

(function executeRule(current, previous /*null when async*/) {
    if (previous.state != 7 && current.state == 7 && String(current.major_incident_state).toLowerCase() == 'accepted') {

        var scheduleTime = new GlideDateTime(); //Time of closure
        var delayDuration = new GlideDuration(); //Delay to add
        delayDuration.setValue("0 24:00:00"); //Delay time
        scheduleTime.add(delayDuration); //Adding delay

        gs.eventQueueScheduled('auto.close.major.incident', current, '', '', scheduleTime); //Queue event
    } else {
    }
})(current, previous);

Event registration

Here you choose which event to fire
We use the following:

Event name auto.close.major.incident
Table Incident [Incident]

None of the other fields have to be set

Script action

This is the action the event registration is using to do XXYY

We use the following:

Name Auto close major incident
Event name Auto.close.major.incident (Name of your event registration)

gs.log('Script Action triggered for ' + current.number, 'MajorIncidentAutoClose');

var gr = new GlideRecord('incident');
if (gr.get(current.sys_id)) {
   
    // Status check
    if (gr.state == 6 && String(gr.major_incident_state).toLowerCase() == 'accepted') {
        gr.state = 7; // Closed
        gr.work_notes = 'Auto-closed after 24 hours in state: Resolved.';
        gr.update();
    }
}


Keep in mind i am no expert in javascript but this will set your major incidents to closed after 24 hours. 
If you would rather have MIs close after a period of time instead of days

Version history
Last update:
‎07-07-2021 03:17 AM
Updated by: