diff --git a/Server-Side Components/Business Rules/Cancel Incomplete Playbooks on Closure/README.md b/Server-Side Components/Business Rules/Cancel Incomplete Playbooks on Closure/README.md new file mode 100644 index 0000000000..9d8f2a1923 --- /dev/null +++ b/Server-Side Components/Business Rules/Cancel Incomplete Playbooks on Closure/README.md @@ -0,0 +1,17 @@ +🛑 Auto-Cancel Playbooks on Incident Deactivation + +Script Type: Business Rule (Server-side) +Trigger: before update +Table: incident +Condition: active changes to false + +📌 Purpose + +Automatically cancel all active Playbooks (sys_pd_context) records associated with an incident when that incident is deactivated (e.g., closed or canceled). + +🧠 Logic + +- Finds all active playbooks (not in completed,canceled) where: +- input_table is incident (or any other table) +- input_record matches current incident’s sys_id +- Cancels each playbook using: sn_playbook.PlaybookExperience.cancelPlaybook(playbookGR, 'Canceled due to the incident closure or cancellation.'); diff --git a/Server-Side Components/Business Rules/Cancel Incomplete Playbooks on Closure/script.js b/Server-Side Components/Business Rules/Cancel Incomplete Playbooks on Closure/script.js new file mode 100644 index 0000000000..59b45b75e5 --- /dev/null +++ b/Server-Side Components/Business Rules/Cancel Incomplete Playbooks on Closure/script.js @@ -0,0 +1,15 @@ +(function executeRule(current, previous /*null when async*/) { + + // Find associated playbooks + var playbookGR = new GlideRecord('sys_pd_context'); + playbookGR.addQuery('input_table', 'incident'); + playbookGR.addQuery('input_record', current.sys_id); + playbookGR.addQuery('state', 'NOT IN', 'completed,canceled'); + playbookGR.query(); + + // Cancel them to avoid hanging context + while (playbookGR.next()) { + sn_playbook.PlaybookExperience.cancelPlaybook(playbookGR, 'Canceled due to the incident closure or cancellation.'); + } + +})(current, previous);