Auto-Close Incident when Incident Tasks are Closed

bvarian
Mega Contributor

I have some Incidents (INC) that contain Incident Tasks (TASK) and would like the Incident to auto-resolve when all Incident Tasks are closed to prevent anyone from having to manually resolve the Incident ticket itself (all actionable items are contained on the tasks...this parent/child is just a grouping construct).

 

I created an "after" Business Rule, but it's not working and I haven't been able to figure out why.  We require close code/close notes so at first I thought an absence of those in my BR script might be the reason, but even after adding those, I'm not getting the change.  I've also tried the state as both '6' and 'Resolved'.

 

If anyone might have suggestions, I'd be very appreciative.  Thanks!

 

find_real_file.png

 

(function executeRule(current, previous /*null when async*/) {

var gr = new GlideRecord('incident_task');

gr.addQuery('incident', current.incident);

gr.addQuery('active', true);

gr.addQuery('sys_id', '!=', current.sys_id);

gr.query();

var count = gr.getRowCount();

if(count == 0)

{

var gr1 = new GlideRecord('incident');

gr1.addQuery('sys_id', current.incident);

gr1.query();

if(gr1.next())

{

gr1.state = '6';
gr1.close_code = 'Solved (Permanently)';
gr1.close_notes = "Auto-Closed based on Incident Tasks being Closed";

gr1.update();

}

}

})(current, previous);

1 ACCEPTED SOLUTION

Prateek kumar
Mega Sage

Can we try something like this.

(function executeRule(current, previous /*null when async*/) {
var count=0;
var tasks = new GlideRecord('incident_task');
tasks.addQuery('parent',current.parent);
tasks.query();
var taskcount = tasks.getRowCount();
while(tasks.next())
{		
if(tasks.state == 3 ||tasks.state == 4 || tasks.state == 7)//change as required
{
count++;
}
}
if(count == taskcount)
{
var inc = new GlideRecord('incident');
inc.addQuery('sys_id',current.parent);
inc.query();
if(inc.next())
{
inc.state = 6;
	inc.update();
	}

}

})(current, previous);

Please mark my response as correct and helpful if it helped solved your question.
-Thanks

View solution in original post

15 REPLIES 15

bvarian
Mega Contributor

Ah, I understand.  First off, yes, I did mean "Closed" for the Incident Tasks (TASK) versus "Resolved".  Sorry about that.  

 

Second, the Incident Tasks are going to an Active=False status, but the parent INC ticket always stays at State=New.

find_real_file.png

 

To true up, here is my BR:

find_real_file.png

 

(function executeRule(current, previous /*null when async*/) {
var gr1 = new GlideRecord('incident_task'); //Find all active child Incident Tasks
gr1.addQuery("incident", gr.sys_id);
gr1.query("active", true);
//gr1.query("state",'Open');
gr1.setLimit(1);
gr1.query();
if (gr1.hasNext()){
gs.addInfoMessage(current.incident.number + " remains open because there are other active Incident Tasks");
//current.setAbortAction(true);
}
else{
var gr = new GlideRecord("incident"); //Find the parent Incident
gr.addQuery("sys_id", current.incident);
gr.setLimit(1);
gr.query();
if (gr.next()) {
gr.state = 6;
gr.close_code = 'Solved (Permanently)';
gr.close_notes = "Auto-Closed based on Incident Tasks being Closed";
gr.update();
}
}
})(current, previous);

In order to better understand what your Business rule is doing, I refactored your code to represent the Incident task query and the Incident modification. Try this in your Business Rule:

(function executeRule(current, previous /*null when async*/) {
    var iTask = new GlideRecord('incident_task'); //Find all active child Incident Tasks
    iTask.addQuery("incident", current.incident); //Where the incident reference field is the same as current record.
    iTask.query("active", true); //and the record is still active
    iTask.setLimit(1);
    iTask.query();
    if (iTask.hasNext()){ //If any open records are found:
        gs.addInfoMessage(current.incident.number + " remains open because there are other active Incident Tasks");
    } else { //When all records are closed:
        var incRec = new GlideRecord("incident"); //Get the parent Incident
        if (incRec.get(current.incident)) {
            incRec.state = 6;
            incRec.close_code = 'Solved (Permanently)';
            incRec.close_notes = "Auto-Closed based on Incident Tasks being Closed";
            incRec.update();
        }
    }
})(current, previous);

Prateek kumar
Mega Sage

Can we try something like this.

(function executeRule(current, previous /*null when async*/) {
var count=0;
var tasks = new GlideRecord('incident_task');
tasks.addQuery('parent',current.parent);
tasks.query();
var taskcount = tasks.getRowCount();
while(tasks.next())
{		
if(tasks.state == 3 ||tasks.state == 4 || tasks.state == 7)//change as required
{
count++;
}
}
if(count == taskcount)
{
var inc = new GlideRecord('incident');
inc.addQuery('sys_id',current.parent);
inc.query();
if(inc.next())
{
inc.state = 6;
	inc.update();
	}

}

})(current, previous);

Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Hello Prateek,

 

I have a question. What script would I need if I needed to convert a WO to an Incident and to automatically cancel WO once the incident is created?

bvarian
Mega Contributor

@Shane J, @Allen A, @Prateek Kumar

Thank all three of you for the patient and excellent suggestions.  Ultimately, Prateek's solution was the one that produced the desired results.  I really love how active the Community is and look forward to hopefully being able to pay it forward in the very near future.  Much gratitude.