From 886cbb93de64e9e288346e5b77e6563f9f8e35b6 Mon Sep 17 00:00:00 2001 From: keshava-palisetti Date: Mon, 27 Oct 2025 21:15:23 +0530 Subject: [PATCH 1/2] Create Readme.md Auto close incident if all related changes are closed --- .../Readme.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Server-Side Components/Business Rules/Auto close incident if all related changes are closed/Readme.md diff --git a/Server-Side Components/Business Rules/Auto close incident if all related changes are closed/Readme.md b/Server-Side Components/Business Rules/Auto close incident if all related changes are closed/Readme.md new file mode 100644 index 0000000000..d3a912e9a5 --- /dev/null +++ b/Server-Side Components/Business Rules/Auto close incident if all related changes are closed/Readme.md @@ -0,0 +1,25 @@ +Business Rule: Auto-Close Incident When All Related Changes Are Closed +Table : change_request +When to Run: After update +Condition: state changes to Closed (or your equivalent "Closed" state number, e.g. state == 3) + +Detailed Working +1. Trigger Point +This After Business Rule runs after a Change Request record is updated. +Specifically, it checks when the state changes to “Closed”. + +2. Check for Related Incident +The script retrieves the incident reference field (incident) from the current change request. +If there’s no linked incident, it skips execution. + +3. Check for Any Remaining Open Change Requests +A new GlideRecord query checks for other Change Requests linked to the same incident where: +If any such records exist, it means not all change requests are closed — so the incident remains open. + +4. Close the Incident Automatically +If no open Change Requests remain, the script: +Fetches the linked incident. +Sets: state = 7 (Closed) + close_code = Auto Closed + close_notes = Auto closure as all changes are closed. +Updates the record. From c93a7aa45bdeba9307dfe542f006e043eb06537c Mon Sep 17 00:00:00 2001 From: keshava-palisetti Date: Mon, 27 Oct 2025 21:17:21 +0530 Subject: [PATCH 2/2] ServiceNow After business rule script Auto close incident if all related change requests are closed --- ...t_if_all_related_change_requests_closed.js | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Server-Side Components/Business Rules/Auto close incident if all related changes are closed/auto_close_incident_if_all_related_change_requests_closed.js diff --git a/Server-Side Components/Business Rules/Auto close incident if all related changes are closed/auto_close_incident_if_all_related_change_requests_closed.js b/Server-Side Components/Business Rules/Auto close incident if all related changes are closed/auto_close_incident_if_all_related_change_requests_closed.js new file mode 100644 index 0000000000..e276e7ca4b --- /dev/null +++ b/Server-Side Components/Business Rules/Auto close incident if all related changes are closed/auto_close_incident_if_all_related_change_requests_closed.js @@ -0,0 +1,35 @@ +(function executeRule(current, previous /*null when async*/) { + // Run only when change_request moves to Closed + if (current.state != previous.state && current.state == 3) { // 3 = Closed + var incidentSysId = current.incident; // assuming there is a reference field to Incident + + if (!incidentSysId) { + gs.info("No related incident found for this change request: " + current.number); + return; + } + + // Query for other open change requests linked to the same incident + var otherCR = new GlideRecord('change_request'); + otherCR.addQuery('incident', incidentSysId); + otherCR.addQuery('sys_id', '!=', current.sys_id); + otherCR.addQuery('state', '!=', 3); // not closed + otherCR.query(); + + if (otherCR.hasNext()) { + gs.info("Incident " + incidentSysId + " still has open change requests. Not closing incident."); + return; + } + + // If no open change requests remain, close the incident + var inc = new GlideRecord('incident'); + if (inc.get(incidentSysId)) { + inc.state = 7; // 7 = Closed (modify as per your instance) + inc.close_code = 'Auto Closed'; + inc.close_notes = 'Incident auto-closed as all associated change requests are closed.'; + inc.update(); + gs.info("Incident " + inc.number + " auto-closed as all related change requests are closed."); + } + + } +})(current, previous); +