- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2023 11:25 PM
Hi Team ,
How to check ,how many incident tasks are closed for respective incident using script
Please guide me with best practice
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2023 11:43 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2023 11:32 PM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2023 11:35 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2023 12:28 AM
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.
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2023 11:37 PM - edited 04-02-2023 11:47 PM
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.