Reflecting CHG Actual End Dates with the latest closed CTask end date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 11:45 AM - edited 11-20-2023 11:47 AM
Not sure if this is even possible but does anyone have a script or suggestion that will allow for the latest closed CTask work end date to also reflect as the actual end date for the CHG request.
The following script is currently active and it works if I only have one closed CTask within the CHG request, however if a user had a few more tasks (specifically more that 2), then it would only reflect up to the second closed task.
Script as follows:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 12:31 PM - edited 11-20-2023 12:32 PM
A simpler approach would be to add the state to the query criteria, and order by work_end descending so that the first record returned is the change task that was closed last:
function executeRule(current, previous /*null when async*/)
//Query change tasks related to the current change request
var tsk = new GlideRecord('change_task');
tsk.addQuery('change_request', current.sys_id);
tsk.addQuery('state', 3);
tsk.orderByDesc('work_end');
tsk.query();
if (tsk.next()) {
current.work_end = tsk.work_end;
current.update();
}
})(current, previous);
I've changed the task var to tsk in case 'task' is a reserved word and could cause issues. The change_request field should be more reliably populated than parent, but maybe not in your environment? Since this script includes current.update(), make sure you're running it after Update.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 02:45 PM
Brad,
Thank you for the quick reply!
Will this work as is or is there any additions needed or tweaking of the code required?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 03:59 PM
It shouldn't need altered. You're running this as a Business Rule on the change_request table after update when State changes to closed or Active changes to false?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 08:07 AM
Yes, that is correct.