How to calculate number of child incident within a parent incident using a Script?

SayaniC51691220
Giga Contributor

Within my project, I want to calculate the number of child incidents attached with a parent incident and also want to add it to a report for my personal use in a dashboard where it will show, all the child incident details attached with parent incident.

1 ACCEPTED SOLUTION

Its_Sagnic
Mega Guru

Hi @SayaniC51691220 ,

You may do it in two ways.

Firstly we may do it in Dashbaord by using Group By Parent and it will show all the child incident in a dropdown format.

Secoundly you may use a glideAggregate to calculate the number of child incident attached with a parent incident and it will exclude the incident where the parent is empty.

var gr = new GlideAggregate('incident');
gr.addQuery('parent','!=','');
gr.addAggregate('COUNT');
gr.groupBy('parent');
gr.query();
while(gr.next()){
	gs.info("Parent Incident "+gr.parent.number + ' contains : ' + gr.getAggregate('COUNT')+" child incident");
}

 

if you find it helpful please mark it as helpful and accept the solution.

Regards,

Sagnic

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron

@SayaniC51691220 

where are you planning to show this?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

yashkamde
Kilo Sage

Hello @SayaniC51691220 ,

Below is the script for calculate the number of child incidents attached with a parent incident :

var gr = new GlideRecord('incident');
gr.addQuery('parent_incident', current.sys_id);
gr.query();

while(gr.next()){
	gs.print(gr.number);
}
gs.print('Number of Child Incidents attached :' + gr.getRowCount());

 

Tweak you logic according to your requirement !!
If my response helped mark as helpful and accept the solution.

 

Tanushree Maiti
Mega Sage

Create a report on the Incident table, where Parent.Task_type = Incident AND Parent is not empty .   

Group by Parent.   

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Sachin_Nasa
Tera Guru

Hi @SayaniC51691220 ,


While GlideAggregate can be useful for backend analysis or validation, it’s also important to look at the reporting and dashboard side of this requirement.

If the goal is to visualize child incidents under their parent incident on a dashboard, this can be achieved without any scripting by creating an Incident report grouped by the Parent Incident field. This allows users to drill down and view all related child incident details directly from the report, providing real-time visibility.

In general, reports and dashboards are the recommended and scalable approach for such use cases, while scripting can be reserved for analysis or one-time checks.

If you find this response helpful, please mark it as Helpful and accept it as the solution if it addresses your question—it would encourage me to contribute more to the ServiceNow Community.

Thanks & regards,
Sachin Narayanasamy

 


Thanks & Regards,
Sachin Narayanasamy