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

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

Hi,
I've tried your code and attaching the relevant screenshot.

SayaniC51691220_0-1770304229084.png

It solved my purpose.

Thanks,
Sayani

Hi @Its_Sagnic 
I've tried your solution and here I'm attaching the relevant screenshot.

SayaniC51691220_0-1770304625347.png

This is helpful, it solved my purpose.

Thanks,
Sayani

Deepak Shaerma
Mega Sage

Hi @SayaniC51691220 

You cannot easily report on "Count of Child Incidents" unless that number is stored in a field on the Parent Incident.

  1. Create a Field:

    Go to the Incident table.
    Create a new integer field named u_child_incident_count (Label: Child Incident Count).
  2. Create a Business Rule:

    Table: Incident [incident]
    When: AFTER
    Insert: Checked | Update: Checked | Delete: Checked
    Filter Conditions: Parent Incident changes (This ensures it only runs when relationships change).

    Advanced Script:

    (function executeRule(current, previous /*null when async*/) {
    
        // 1. Identify the Parent(s) we need to update
        // We check both current and previous to handle cases where an incident is moved FROM one parent TO another.
        var parentsToUpdate = [];
        
        if (current.parent_incident && !current.parent_incident.nil()) {
            parentsToUpdate.push(current.parent_incident.toString());
        }
        if (previous.parent_incident && !previous.parent_incident.nil() && previous.parent_incident.toString() !== current.parent_incident.toString()) {
            parentsToUpdate.push(previous.parent_incident.toString());
        }
    
        // Deduplicate the array (optional but good practice)
        var uniqueParents = new ArrayUtil().unique(parentsToUpdate);
    
        // 2. Loop through parents and recalculate count
        for (var i = 0; i < uniqueParents.length; i++) {
            var parentID = uniqueParents[i];
            
            var agg = new GlideAggregate('incident');
            agg.addQuery('parent_incident', parentID);
            agg.addAggregate('COUNT');
            agg.query();
            
            var count = 0;
            if (agg.next()) {
                count = agg.getAggregate('COUNT');
            }
            
            // 3. Update the Parent Record
            var parentInc = new GlideRecord('incident');
            if (parentInc.get(parentID)) {
                parentInc.u_child_incident_count = count;
                // setWorkflow(false) prevents this update from triggering other recursive business rules
                parentInc.setWorkflow(false); 
                parentInc.update();
            }
        }
    
    })(current, previous);

     

    then create a standard list report on the Incident table

     

    Happy to help! ‌‌
    To help others in the community find this solution, kindly mark this response as the Correct Answer ‌‌ and Helpful‌‌.
    Warm Regards,
    Deepak Sharma
    Community Rising Star 2025