Prevent Closure of a Parent Incident if It Has Open Child Incidents

arshadansar
Tera Contributor

Don't Know why its not working. 

current.setAbortAction(true); 
 
arshadansar_0-1748089473694.pngarshadansar_2-1748089524575.png

 

arshadansar_1-1748089509632.png

 

Want not closed incident if child incident is open.

1 ACCEPTED SOLUTION

Hello @arshadansar ,

 

Ok, got it, thanks. Please update your Business Rule script as follows:

 

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

    var childTask = new GlideRecord('incident');
    childTask.addQuery('parent_incident', current.getUniqueValue());
    childTask.addQuery('state', '!=', '7'); // not closed
    childTask.query();

    var arr = [];
    while (childTask.next()) {
        arr.push(childTask.getValue('number'));
    }

    if (arr.length > 0) {
        gs.addErrorMessage('Cannot update the parent Incident until all child Incidents are closed: ' + arr.join(', '));
        current.state = previous.state;
        current.incident_state = previous.incident_state;
        current.setAbortAction(true);
    }

})(current, previous);

 

And make sure the BR "When" field is set to "before", not "after".

 

Regards,

Robert

View solution in original post

6 REPLIES 6

Robert H
Mega Sage

Hello @arshadansar ,

 

I don't understand the question.

You have a Business Rule that prevents closing an Incident if it has open child Incidents.

And your screen shots are demonstrating that it's working fine: your Incident 8112 has two open child Incidents, that's why you cannot close it. Once you close these two Incidents your Business Rule will allow you to close the parent Incident.

 

PS: the second GlideRecord query is unnecessary. Instead of "if (childTask.hasNext())" you can do "while (childTask.next())" and populate the array right away. And then add an "if (arr.length > 0)" at the end to check if you need to print the message and abort the action.

 

Regards,

Robert

All Work Fine But when i try to close parent incident it show me message that you can't close parent incident but after it state changes to close that i don't want to update state if child is present.

arshadansar_0-1748095179659.png

 

Hi @arshadansar 

I hope it's changed only in the UI. If you refresh the screen, the state will revert to "Resolved" state.

If not, then try the below script...

 

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

var records = new GlideRecord('incident');
records.addQuery('parent_incident', current.sys_id);
records.addQuery('state', '!=', 7); // 7 is the state for 'Closed'
records.setLimit(1);
records.query();

if (records.hasNext()) {
var childrenIncidentNumbers = [];
while (records.next()) {
childrenIncidentNumbers.push(records.getValue('number'));
}
gs.addErrorMessage('Cannot update parent incident ' + current.number + ' as there are open children incidents: ' + childrenIncidentNumbers.join(', '));
current.setAbortAction(true);
}

})(current, previous);

 

Regards ,

Siva

Hello @arshadansar ,

 

Ok, got it, thanks. Please update your Business Rule script as follows:

 

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

    var childTask = new GlideRecord('incident');
    childTask.addQuery('parent_incident', current.getUniqueValue());
    childTask.addQuery('state', '!=', '7'); // not closed
    childTask.query();

    var arr = [];
    while (childTask.next()) {
        arr.push(childTask.getValue('number'));
    }

    if (arr.length > 0) {
        gs.addErrorMessage('Cannot update the parent Incident until all child Incidents are closed: ' + arr.join(', '));
        current.state = previous.state;
        current.incident_state = previous.incident_state;
        current.setAbortAction(true);
    }

})(current, previous);

 

And make sure the BR "When" field is set to "before", not "after".

 

Regards,

Robert