|
| 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); |
| 8 | + ciRel.query(); |
| 9 | + |
| 10 | + while (ciRel.next()) { |
| 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(); |
| 21 | + |
| 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 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | +})(current, previous); |
0 commit comments