How many incident tasks are closed for incident

String
Kilo Sage

Hi Team ,

How to check ,how many incident tasks are closed for respective incident using script 

 

Please guide me with best practice 

1 ACCEPTED SOLUTION

nitinsharma2510
Giga Guru

Hey @String ,

You can use the following script for finding the number of closed Incident Tasks for an Incident.

//incident_task is the table name for Incident Task table
var target = new GlideRecord('incident_task');
//replace with incident=sys_id of your incident
//states 3,4,7 correspond to closed complete, closed incomplete & closed skipped respectively
target.addEncodedQuery("stateIN3,4,7^incident=077b6755dbd26c10a6a84de239961911");
target.query();
//getRowCount() returns the number of records returned by the query
gs.info("Number of open Incident Tasks are = " + target.getRowCount());

I would suggest substituting the fields with your data and running it in the 'Scripts - Background' module.

View solution in original post

5 REPLIES 5

OlaN
Giga Sage
Giga Sage

Hi,

Could you please clarify a bit on your use case?

At what point do you want to check how many incident tasks that are closed?

For example, should the check happen when the incident is updated, or when the incident task is updated?

Hi @OlaN 

we are receiving the inbound update ,So I have to resolve(state) the incident only when all the incident tasks are in closed state .

So need to check all the tasks are closed for respective incident or not 

In that case I would do a business rule that looks something like this;

Do note, that this example does run on all attempts to set the incident to resolved.

I can imagine that you might need to consider if allowing the end user to close their own incidents, so you might need to add some additional conditions.

check-incident-task-before-close-1.png

 

check-incident-task-before-close-2.png

 

Script:

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

	var incTaskGR = new GlideRecord('incident_task');
	incTaskGR.addQuery('incident', current.getUniqueValue());
	incTaskGR.addActiveQuery();
	incTaskGR.query();
	
	if (incTaskGR.hasNext()){
		gs.addErrorMessage('Cannot resolve incident, is still has open incident tasks');
		current.setAbortAction(true);
	}
	
})(current, previous);

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @String ,

                              You can try below script to find out closed count task of respective incident in background script.

 

// Get the current incident record
var incident = new GlideRecord('incident');
incident.get('sys_id', '<incident_sys_id>');

// Query the Incident Task table for closed tasks related to the incident
var taskGr = new GlideAggregate('incident_task');
taskGr.addQuery('incident', incident.sys_id);
taskGr.addQuery('active', false);
taskGr.addQuery('state', '3');
taskGr.addAggregate('COUNT');
taskGr.query();

// Get the count of closed tasks
var closedTaskCount = 0;
if (taskGr.next()) {
  closedTaskCount = taskGr.getAggregate('COUNT');
}

gs.info('Closed tasks for incident ' + incident.number + ': ' + closedTaskCount);

 

Kindly mark correct and helpful if applicable.