|
1 | | -// Business Rule: After Update on change_request |
2 | | -// Condition: affected_ci field has changed |
3 | | -(function executeRule(current, previous /*null for insert*/) { |
4 | | - // Suppose affected CI list is in a related list table change_request_ci |
5 | | - var ciRel = new GlideRecord('change_request_ci'); |
6 | | - ciRel.addQuery('change_request', current.sys_id); |
| 1 | +// Business Rule: After Insert or After Update on change_request |
| 2 | +// Purpose: Create Change Tasks for each affected CI and notify its owner |
| 3 | +(function executeRule(current, previous /*null when insert*/) { |
| 4 | + |
| 5 | + // Query the task_ci table to get all CIs linked to this Change Request |
| 6 | + var ciRel = new GlideRecord('task_ci'); |
| 7 | + ciRel.addQuery('task', current.sys_id); |
7 | 8 | ciRel.query(); |
8 | 9 |
|
9 | 10 | while (ciRel.next()) { |
10 | | - var ci = ciRel.cmdb_ci.getRefRecord(); |
11 | | - if (ci.owner) { |
12 | | - // create change_task (task table) record for owner |
13 | | - var ct = new GlideRecord('change_task'); |
14 | | - ct.initialize(); |
15 | | - ct.change_request = current.sys_id; |
16 | | - ct.cmdb_ci = ci.sys_id; |
17 | | - ct.assigned_to = ci.owner; |
18 | | - ct.short_description = 'Review Change for your CI: ' + ci.name; |
19 | | - ct.insert(); |
| 11 | + var ci = ciRel.ci_item.getRefRecord(); // Fetch the actual CI record |
| 12 | + |
| 13 | + if (ci.owner) { // Proceed only if CI has an owner |
| 14 | + |
| 15 | + // Check if a Change Task for this CI and owner already exists |
| 16 | + var existingTask = new GlideRecord('change_task'); |
| 17 | + existingTask.addQuery('change_request', current.sys_id); |
| 18 | + existingTask.addQuery('cmdb_ci', ci.sys_id); |
| 19 | + existingTask.addQuery('assigned_to', ci.owner); |
| 20 | + existingTask.query(); |
20 | 21 |
|
21 | | - // send notification – could simply use gs.eventQueue or similar |
22 | | - gs.eventQueue('change.ci.owner.notification', ct, ci.owner, current.sys_id); |
| 22 | + if (!existingTask.next()) { |
| 23 | + // Create new Change Task for CI Owner |
| 24 | + var ct = new GlideRecord('change_task'); |
| 25 | + ct.initialize(); |
| 26 | + ct.change_request = current.sys_id; |
| 27 | + ct.cmdb_ci = ci.sys_id; |
| 28 | + ct.assigned_to = ci.owner; |
| 29 | + ct.short_description = 'Review Change for your CI: ' + ci.name; |
| 30 | + ct.insert(); |
| 31 | + |
| 32 | + // Trigger a notification event |
| 33 | + gs.eventQueue('change.ci.owner.notification', ct, ci.owner, current.sys_id); |
| 34 | + } |
23 | 35 | } |
24 | 36 | } |
| 37 | + |
25 | 38 | })(current, previous); |
0 commit comments