How to Close Parent Incident if all child Incidents are closed.

swechchha
Tera Contributor

Hello Everyone,

 

I have a requirement, If Parent incident is closed, all associated child incidents should get auto close. 

How to Achieve this through Business rule. Please Provide Solution.

2 ACCEPTED SOLUTIONS

Sandeep Rajput
Tera Patron
Tera Patron

@swechchha You have mentioned conflicting requirements in the short description and description of this thread.

 

In short description you have mentioned "How to Close Parent Incident if all child Incidents are closed." whereas in the short description you have mentioned "If Parent incident is closed, all associated child incidents should get auto close. "

 

1. Solution Close parent incident if all child incidents are closed.

 

You need to create an onBefore update business rule on incident table with condition State changes to Closed

Here is the script you can use inside the business rule.

(function executeRule(current, previous /*null when async*/) {
    // Check if the current record has a parent
    if (current.parent) {
        var parentIncident = new GlideRecord('incident');
        if (parentIncident.get(current.parent)) {
            // Check if all child incidents are closed
            var childIncidents = new GlideAggregate('incident');
            childIncidents.addQuery('parent', current.parent);
            childIncidents.addQuery('state', '!=', '7'); // 7 = Closed
            childIncidents.addAggregate('COUNT');
            childIncidents.query();

            if (!childIncidents.hasNext() || childIncidents.getAggregate('COUNT') == 0) {
                parentIncident.state = '7'; // Closed state
                parentIncident.update();
            }
        }
    }
})(current, previous);

 

Solution: If Parent incident is closed, all associated child incidents should get auto close

 

To achieve this, you need to create an onAfter update business rule with following conditions.

1. State changes to Closed

AND

2. Parent is empty

 

Along with this you need to put following script in the business rule.

 

(function executeRule(current, previous /*null when async*/) {
    // Ensure this is a parent incident
    var childIncidents = new GlideRecord('incident');
    childIncidents.addQuery('parent', current.sys_id); // Find child incidents of this parent
    childIncidents.addQuery('state', '!=', '7'); // Ensure only open incidents are processed (7 = Closed)
    childIncidents.query();

    while (childIncidents.next()) {
        childIncidents.state = '7'; // Set state to Closed
        childIncidents.update();
    }
})(current, previous);

Hope this helps.

View solution in original post

PritamG
Mega Guru

when to run : After(update)

condition: current.state == 'Closed'

(function executeRule(current, previous) {
    var gr = new GlideRecord('incident');
    gr.addQuery('parent', current.sys_id);
    gr.addQuery('state', '!=', 'Closed');
    gr.query();
    while (gr.next()) {
        gr.state = current.state;  // Set state to "Closed" or use corresponding state number
        gr.update();
    }
})(current, previous);

Fetches only child incidents of the current parent that are not already closed and Uses a loop to update state for each child incident. When the parent is closed, all linked child incidents automatically close without extra manual intervention.

View solution in original post

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@swechchha You have mentioned conflicting requirements in the short description and description of this thread.

 

In short description you have mentioned "How to Close Parent Incident if all child Incidents are closed." whereas in the short description you have mentioned "If Parent incident is closed, all associated child incidents should get auto close. "

 

1. Solution Close parent incident if all child incidents are closed.

 

You need to create an onBefore update business rule on incident table with condition State changes to Closed

Here is the script you can use inside the business rule.

(function executeRule(current, previous /*null when async*/) {
    // Check if the current record has a parent
    if (current.parent) {
        var parentIncident = new GlideRecord('incident');
        if (parentIncident.get(current.parent)) {
            // Check if all child incidents are closed
            var childIncidents = new GlideAggregate('incident');
            childIncidents.addQuery('parent', current.parent);
            childIncidents.addQuery('state', '!=', '7'); // 7 = Closed
            childIncidents.addAggregate('COUNT');
            childIncidents.query();

            if (!childIncidents.hasNext() || childIncidents.getAggregate('COUNT') == 0) {
                parentIncident.state = '7'; // Closed state
                parentIncident.update();
            }
        }
    }
})(current, previous);

 

Solution: If Parent incident is closed, all associated child incidents should get auto close

 

To achieve this, you need to create an onAfter update business rule with following conditions.

1. State changes to Closed

AND

2. Parent is empty

 

Along with this you need to put following script in the business rule.

 

(function executeRule(current, previous /*null when async*/) {
    // Ensure this is a parent incident
    var childIncidents = new GlideRecord('incident');
    childIncidents.addQuery('parent', current.sys_id); // Find child incidents of this parent
    childIncidents.addQuery('state', '!=', '7'); // Ensure only open incidents are processed (7 = Closed)
    childIncidents.query();

    while (childIncidents.next()) {
        childIncidents.state = '7'; // Set state to Closed
        childIncidents.update();
    }
})(current, previous);

Hope this helps.

JaishreeR
Giga Guru

Hi @swechchha 

 

You can also user flow designer for the same purpose.

 

If my response was helpful, please mark my answer helpful 👍 and accept as a solution  so it can be valuable to others.

 

Thanks 

Jaishree R

PritamG
Mega Guru

when to run : After(update)

condition: current.state == 'Closed'

(function executeRule(current, previous) {
    var gr = new GlideRecord('incident');
    gr.addQuery('parent', current.sys_id);
    gr.addQuery('state', '!=', 'Closed');
    gr.query();
    while (gr.next()) {
        gr.state = current.state;  // Set state to "Closed" or use corresponding state number
        gr.update();
    }
})(current, previous);

Fetches only child incidents of the current parent that are not already closed and Uses a loop to update state for each child incident. When the parent is closed, all linked child incidents automatically close without extra manual intervention.

Ankur Bawiskar
Tera Patron
Tera Patron

@swechchha 

this should be an easy requirement with BR or flow designer, what did you start with and where are you stuck?

I have shared a working example below using flow designer

Close parent incident when all child incidents are closed

If all child incident is closed then after Parent incident should be closed. How to achieve through ...

If my response helped please mark it correct and close the thread so that it benefits future readers.

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