From 89cec720db8d08d8faa8a0e39ed05fe201915c76 Mon Sep 17 00:00:00 2001 From: Naveen Kumar <103413520+naveensnow@users.noreply.github.com> Date: Fri, 3 Oct 2025 17:51:37 +0530 Subject: [PATCH 1/5] Create scriptBR.js --- .../scriptBR.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/scriptBR.js diff --git a/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/scriptBR.js b/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/scriptBR.js new file mode 100644 index 0000000000..e6f6118d4a --- /dev/null +++ b/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/scriptBR.js @@ -0,0 +1,21 @@ +(function executeRule(current, previous /*null when async*/ ) { + + // Query for any child incidents related to the current parent incident. + var childIncidents = new GlideRecord('incident'); + childIncidents.addQuery('parent_incident', current.sys_id); + childIncidents.addEncodedQuery('state!=6^ORstate!=7^ORstate!=8'); // state is not closed,Resoved,cancelled + childIncidents.addActiveQuery(); + childIncidents.query(); + // Check if any open child incidents were found. + if (childIncidents.getRowCount() > 0) { + var childNumbers = []; + while (childIncidents.next()) { + childNumbers.push(childIncidents.number.toString()); + } + // Display an error message with the open child incident numbers. + gs.addErrorMessage('Cannot close this incident. Please close the following child incidents first: ' + childNumbers.join(', ')); + //prevent saving the record + current.setAbortAction(true); + } + +})(current, previous); From d13be201702a94dcae1dbe1f2eb2a64dccee97f6 Mon Sep 17 00:00:00 2001 From: Naveen Kumar <103413520+naveensnow@users.noreply.github.com> Date: Fri, 3 Oct 2025 17:55:58 +0530 Subject: [PATCH 2/5] Create README.md --- .../README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/README.md diff --git a/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/README.md b/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/README.md new file mode 100644 index 0000000000..33ac8238f1 --- /dev/null +++ b/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/README.md @@ -0,0 +1,16 @@ +This business rule is designed for ServiceNow to prevent a parent incident from being closed or resolved while it still has active child incidents. +If a user attempts to set the parent incident's state to "Resolved," "Closed," or "Cancelled," the rule will query for any related child incidents that are still open. +If open children are found, the update will be aborted, and an error message will be displayed to the user. + +Navigate to System Definition > Business Rules in the ServiceNow filter navigator. +Click New. +Fill out the form with the following details: +Name: Prevent Parent Closure with Open Children +Table: Incident [incident] +Advanced: true +When: before +Update: Check this box. +In the When to run tab, set the Condition field: +current.state.changesTo(7) || current.state.changesTo(6) || current.state.changesTo(8) +Note: The state values (6, 7, 8) may vary based on your instance configuration. +In the Advanced tab, paste the provided script into the Script field. From a145eedc9c7fda075af4529b628a749c26ba647d Mon Sep 17 00:00:00 2001 From: Naveen Kumar <103413520+naveensnow@users.noreply.github.com> Date: Fri, 3 Oct 2025 18:25:15 +0530 Subject: [PATCH 3/5] Update scriptBR.js --- .../scriptBR.js | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/scriptBR.js b/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/scriptBR.js index e6f6118d4a..4d2f5d092b 100644 --- a/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/scriptBR.js +++ b/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/scriptBR.js @@ -1,21 +1,26 @@ -(function executeRule(current, previous /*null when async*/ ) { +(function executeRule(current, previous /*null when async*/) { + + + //When the Incident state values are set to: 6 (Resolved), 7 (Closed), 8 (Cancelled). + // The `previous.state` check prevents the script from running when a closed ticket is re-closed. + if ((current.state == '6' || current.state == '7' || current.state == '8') && current.state != previous.state) { - // Query for any child incidents related to the current parent incident. - var childIncidents = new GlideRecord('incident'); - childIncidents.addQuery('parent_incident', current.sys_id); - childIncidents.addEncodedQuery('state!=6^ORstate!=7^ORstate!=8'); // state is not closed,Resoved,cancelled - childIncidents.addActiveQuery(); - childIncidents.query(); - // Check if any open child incidents were found. - if (childIncidents.getRowCount() > 0) { - var childNumbers = []; - while (childIncidents.next()) { - childNumbers.push(childIncidents.number.toString()); + // Use GlideAggregate to efficiently count child incidents that are not yet closed. + var ga = new GlideAggregate('incident'); + ga.addQuery('parent_incident', current.sys_id); + ga.addActiveQuery(); + ga.addAggregate('COUNT'); + ga.query(); + var childCount = 0; + if (ga.next()) { + // Retrieve the aggregated count. + childCount = ga.getAggregate('COUNT'); + } + // If open child incidents are found, abort the parent's closure and display an error. + if (childCount > 0) { + gs.addErrorMessage('Cannot close this incident. ' + childCount + ' child incidents are still open.'); + current.setAbortAction(true); } - // Display an error message with the open child incident numbers. - gs.addErrorMessage('Cannot close this incident. Please close the following child incidents first: ' + childNumbers.join(', ')); - //prevent saving the record - current.setAbortAction(true); } })(current, previous); From 804fac902e7bf53324699091fa77e716daa860d4 Mon Sep 17 00:00:00 2001 From: Naveen Kumar <103413520+naveensnow@users.noreply.github.com> Date: Fri, 3 Oct 2025 18:26:27 +0530 Subject: [PATCH 4/5] Update README.md --- .../Abort Parent Incident Closure When Child is Open/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/README.md b/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/README.md index 33ac8238f1..d8d29f1150 100644 --- a/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/README.md +++ b/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/README.md @@ -11,6 +11,6 @@ Advanced: true When: before Update: Check this box. In the When to run tab, set the Condition field: -current.state.changesTo(7) || current.state.changesTo(6) || current.state.changesTo(8) +current.state.changesTo(7) || current.state.changesTo(6) || current.state.changesTo(8) The state values are: 6 (Resolved), 7 (Closed), 8 (Cancelled). Note: The state values (6, 7, 8) may vary based on your instance configuration. In the Advanced tab, paste the provided script into the Script field. From 98c3ef412ca0a65f582550b85f57298b4886294d Mon Sep 17 00:00:00 2001 From: Naveen Kumar <103413520+naveensnow@users.noreply.github.com> Date: Fri, 3 Oct 2025 18:27:44 +0530 Subject: [PATCH 5/5] Update README.md --- .../Abort Parent Incident Closure When Child is Open/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/README.md b/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/README.md index d8d29f1150..aa5dcb5fa2 100644 --- a/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/README.md +++ b/Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open/README.md @@ -11,6 +11,6 @@ Advanced: true When: before Update: Check this box. In the When to run tab, set the Condition field: -current.state.changesTo(7) || current.state.changesTo(6) || current.state.changesTo(8) The state values are: 6 (Resolved), 7 (Closed), 8 (Cancelled). +current.state.changesTo(7) || current.state.changesTo(6) || current.state.changesTo(8) //The state values are: 6 (Resolved), 7 (Closed), 8 (Cancelled). Note: The state values (6, 7, 8) may vary based on your instance configuration. In the Advanced tab, paste the provided script into the Script field.