diff --git a/Server-Side Components/Background Scripts/Update Parent Incident State To Closed If All Child Incident Is Closed/README.md b/Server-Side Components/Background Scripts/Update Parent Incident State To Closed If All Child Incident Is Closed/README.md new file mode 100644 index 0000000000..0f3adf1591 --- /dev/null +++ b/Server-Side Components/Background Scripts/Update Parent Incident State To Closed If All Child Incident Is Closed/README.md @@ -0,0 +1,4 @@ +This Background Script provides administrators with a quick and reliable method to: ++Check whether all child incidents under a parent are closed. ++Automatically close the parent if all children are resolved. ++Ensure consistent closure logic without manual intervention. diff --git a/Server-Side Components/Background Scripts/Update Parent Incident State To Closed If All Child Incident Is Closed/script.js b/Server-Side Components/Background Scripts/Update Parent Incident State To Closed If All Child Incident Is Closed/script.js new file mode 100644 index 0000000000..1fab41772f --- /dev/null +++ b/Server-Side Components/Background Scripts/Update Parent Incident State To Closed If All Child Incident Is Closed/script.js @@ -0,0 +1,29 @@ +(function() { + var parentSysId = 'a83820b58f723300e7e16c7827bdeed2'; // Replace with actual parent incident sys_id + var childIncidents = new GlideRecord('incident'); + childIncidents.addQuery('parent_incident', parentSysId); + childIncidents.query(); + var allClosed = true; + while (childIncidents.next()) { + if (childIncidents.state != 7) { // 7 = Closed + allClosed = false; + gs.info("Child incident " + childIncidents.number + " is still open."); + } + } + if (allClosed) { + var parent = new GlideRecord('incident'); + if (parent.get(parentSysId)) { + parent.state = 7; // Closed + parent.close_code = 'Duplicate'; // Adjust to valid closure code in your instance + parent.close_notes = 'Automatically closed since all child incidents are closed.'; + parent.setWorkflow(false); // Prevent triggering BRs/workflows + parent.update(); + + gs.info("Parent incident [" + parent.number + "] has been closed automatically."); + } else { + gs.warn("Parent incident not found for sys_id: " + parentSysId); + } + } else { + gs.info("Parent not closed because one or more child incidents are still open."); + } +})();