From cc6fd7f01016ed718a2bd3dd4f2fe531a0ea633f Mon Sep 17 00:00:00 2001 From: srikanthroyal932-cmyk Date: Sun, 26 Oct 2025 21:44:38 +0530 Subject: [PATCH 1/3] BRScript.js --- .../BRScript.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Server-Side Components/Business Rules/Auto-assign and notify owners of Affected CIs/BRScript.js diff --git a/Server-Side Components/Business Rules/Auto-assign and notify owners of Affected CIs/BRScript.js b/Server-Side Components/Business Rules/Auto-assign and notify owners of Affected CIs/BRScript.js new file mode 100644 index 0000000000..5f0ded458d --- /dev/null +++ b/Server-Side Components/Business Rules/Auto-assign and notify owners of Affected CIs/BRScript.js @@ -0,0 +1,25 @@ +// Business Rule: After Update on change_request +// Condition: affected_ci field has changed +(function executeRule(current, previous /*null for insert*/) { + // Suppose affected CI list is in a related list table change_request_ci + var ciRel = new GlideRecord('change_request_ci'); + ciRel.addQuery('change_request', current.sys_id); + ciRel.query(); + + while (ciRel.next()) { + var ci = ciRel.cmdb_ci.getRefRecord(); + if (ci.owner) { + // create change_task (task table) record for owner + var ct = new GlideRecord('change_task'); + ct.initialize(); + ct.change_request = current.sys_id; + ct.cmdb_ci = ci.sys_id; + ct.assigned_to = ci.owner; + ct.short_description = 'Review Change for your CI: ' + ci.name; + ct.insert(); + + // send notification – could simply use gs.eventQueue or similar + gs.eventQueue('change.ci.owner.notification', ct, ci.owner, current.sys_id); + } + } +})(current, previous); From 43d5c3cbe7b89e9b86f0306b087f2c437ea1aa28 Mon Sep 17 00:00:00 2001 From: srikanthroyal932-cmyk Date: Sun, 26 Oct 2025 21:59:36 +0530 Subject: [PATCH 2/3] README.md --- .../README.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Server-Side Components/Business Rules/Auto-assign and notify owners of Affected CIs/README.md diff --git a/Server-Side Components/Business Rules/Auto-assign and notify owners of Affected CIs/README.md b/Server-Side Components/Business Rules/Auto-assign and notify owners of Affected CIs/README.md new file mode 100644 index 0000000000..e15ccde789 --- /dev/null +++ b/Server-Side Components/Business Rules/Auto-assign and notify owners of Affected CIs/README.md @@ -0,0 +1,8 @@ +Create Change Tasks for each affected CI and notify its owner + +1. Create a Business Rule - After Insert/Update Change Request Table +2. Query the task_ci table to get all CIs linked to this Change Request +4. Fetch all the actual CI records present in the table +5. Proceed if CI has a owner, check if a Change Task for this CI and owner already exists +6. If not existing create a change task for CI owner +7. Triggers an event to notify the CI owner (email/push). From d8ea8e1af390871b5af1511b63cd6768a5ffa3d3 Mon Sep 17 00:00:00 2001 From: srikanthroyal932-cmyk Date: Sun, 26 Oct 2025 22:00:45 +0530 Subject: [PATCH 3/3] BRScript.js --- .../BRScript.js | 49 ++++++++++++------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/Server-Side Components/Business Rules/Auto-assign and notify owners of Affected CIs/BRScript.js b/Server-Side Components/Business Rules/Auto-assign and notify owners of Affected CIs/BRScript.js index 5f0ded458d..3c273a8d50 100644 --- a/Server-Side Components/Business Rules/Auto-assign and notify owners of Affected CIs/BRScript.js +++ b/Server-Side Components/Business Rules/Auto-assign and notify owners of Affected CIs/BRScript.js @@ -1,25 +1,38 @@ -// Business Rule: After Update on change_request -// Condition: affected_ci field has changed -(function executeRule(current, previous /*null for insert*/) { - // Suppose affected CI list is in a related list table change_request_ci - var ciRel = new GlideRecord('change_request_ci'); - ciRel.addQuery('change_request', current.sys_id); +// Business Rule: After Insert or After Update on change_request +// Purpose: Create Change Tasks for each affected CI and notify its owner +(function executeRule(current, previous /*null when insert*/) { + + // Query the task_ci table to get all CIs linked to this Change Request + var ciRel = new GlideRecord('task_ci'); + ciRel.addQuery('task', current.sys_id); ciRel.query(); while (ciRel.next()) { - var ci = ciRel.cmdb_ci.getRefRecord(); - if (ci.owner) { - // create change_task (task table) record for owner - var ct = new GlideRecord('change_task'); - ct.initialize(); - ct.change_request = current.sys_id; - ct.cmdb_ci = ci.sys_id; - ct.assigned_to = ci.owner; - ct.short_description = 'Review Change for your CI: ' + ci.name; - ct.insert(); + var ci = ciRel.ci_item.getRefRecord(); // Fetch the actual CI record + + if (ci.owner) { // Proceed only if CI has an owner + + // Check if a Change Task for this CI and owner already exists + var existingTask = new GlideRecord('change_task'); + existingTask.addQuery('change_request', current.sys_id); + existingTask.addQuery('cmdb_ci', ci.sys_id); + existingTask.addQuery('assigned_to', ci.owner); + existingTask.query(); - // send notification – could simply use gs.eventQueue or similar - gs.eventQueue('change.ci.owner.notification', ct, ci.owner, current.sys_id); + if (!existingTask.next()) { + // Create new Change Task for CI Owner + var ct = new GlideRecord('change_task'); + ct.initialize(); + ct.change_request = current.sys_id; + ct.cmdb_ci = ci.sys_id; + ct.assigned_to = ci.owner; + ct.short_description = 'Review Change for your CI: ' + ci.name; + ct.insert(); + + // Trigger a notification event + gs.eventQueue('change.ci.owner.notification', ct, ci.owner, current.sys_id); + } } } + })(current, previous);