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

OK, I tried adding a gs.addInfoMessage("Starting BR"); to the Business Rule and as I suspected from not seeing it on "System Logs - All", it doesn't appear to be running at all. 

 

The BR is active, is set to after / update and is scoped to Global.  Its "when to run" is after/update/closure states of incident tasks. 

 

All I can figure is that something in the script syntax itself is wrong.  <sad face> 

gs.addInfoMessage would show up as a banner on top after you Save/Update the Incident Task, not in the logs.

 

You are running this on Incident Task table, not Incident right?  Have you tried it as a 'before' rule?  What Order is it set to?

 

Edit:  Post a screen shot of your BR!

Edit2:  It's like I'm not even reading your orig post...  I'm going to try to get this to work in my dev env today and will get back to you.

Shane J
Tera Guru

I think the attached will work for you (worked for me in my developer instance).

 

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.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);

bvarian
Mega Contributor

Hmmmm.  So this behaves interestingly, Shane.  

 

If I'm working an Incident Task (TASK) record, closing it takes me to the parent Incident (INC) ticket, where I see the gs.addInfoMessage text.

find_real_file.png

I can repeat that exercise for any additional Incident Task (TASK) tickets.

 

However, the parent Incident (INC) ticket never changes to a Resolved state (6) after the last one of those TASK tickets is resolved.  

 

I had a BR that was already warning producing a gs.addErrorMessage warning if someone tried to close the parent INC if any child TASK tickets were still open.  Seeing that you tried to combine both those functions here, I sat that BR to false before I tested this, just to be sure they didn't cross wires.  

What's you're seeing with the addInfoMessage is expected (it should show up whenever the BR runs, you can just comment that out if you don't want it to show).

 

However, the parent Incident (INC) ticket never changes to a Resolved state (6) after the last one of those TASK tickets is resolved

Did you mean closed?  I'm not sure where the failure on your end would be occurring, this is working as I'd expect it to assuming your Incident Tasks are being set to Active = false when they are being some version of 'closed'.  If not, that could be the issue - you'd need to update the query to look at State instead of Active for the Incident Tasks.