- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2023 08:34 AM
Helo all,
When the catalog task is closed, the script below will be executed. It will check to see whether there are any active child tasks. If other active child task found, only update the work notes. Otherwise, update the state and work notes if of RITM. I tested, however it kept saying "Child found" even if all of the child tasks were closed.
RunUpdate();
function RunUpdate() {
var gr = new GlideRecord('sc_task');
gr.addQuery('request_item', current.request_item);
gr.addQuery('active', 'true');
gr.addAggregate('count');
gr.query();
gr.get(event.instance);
gs.getSession().impersonate('system');
var task = "[code]<a href='/" + current.sys_class_name + ".do?sys_id=" + current.sys_id + "'>" + current.number + "</a>[/code]";
var grReq = current.request_item.getRefRecord();
if (gr.next()) {
var count = sc_task.getAggregate('count');
gs.log("Count of active task " + count);
}
if (count < 1) {
gs.log("No Child found");
//Found active child task
grReq.work_notes = gs.getMessage("Request Task {0} has been closed", task);
grReq.state = 2;
} else {
gs.log("Child found");
grReq.work_notes = gs.getMessage("Request Task {0} has been closed", task);
grReq.update();
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2023 09:36 AM - edited ‎10-07-2023 09:37 AM
@tsoct Can you update the code as follows and check if it works for you.
RunUpdate();
function RunUpdate() {
var gr = new GlideAggregate('sc_task');
gr.addQuery('request_item', current.request_item);
gr.addQuery('active', 'true');
gr.addAggregate('count');
gr.query();
//gr.get(event.instance);
gs.getSession().impersonate('system');
var task = "[code]<a href='/" + current.sys_class_name + ".do?sys_id=" + current.sys_id + "'>" + current.number + "</a>[/code]";
var grReq = current.request_item.getRefRecord();
var count=0;
if (gr.next()) {
count = sc_task.getAggregate('count');
gs.log("Count of active task " + count);
}
if (count < 1) {
gs.log("No Child found");
//Found active child task
grReq.work_notes = gs.getMessage("Request Task {0} has been closed", task);
grReq.state = 2;
} else {
gs.log("Child found");
grReq.work_notes = gs.getMessage("Request Task {0} has been closed", task);
grReq.update();
}
}
Hope this helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2023 09:19 AM
@tsoct Does this code reside in a script action? If yes then could you share the code which is triggering the event.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2023 09:25 AM
Yes. There is a before Update Business rule used to trigger the event.
(function executeRule(current, previous /*null when async*/ ) {
gs.eventQueue('updateRITM.record.stateupdate', current);
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2023 09:36 AM - edited ‎10-07-2023 09:37 AM
@tsoct Can you update the code as follows and check if it works for you.
RunUpdate();
function RunUpdate() {
var gr = new GlideAggregate('sc_task');
gr.addQuery('request_item', current.request_item);
gr.addQuery('active', 'true');
gr.addAggregate('count');
gr.query();
//gr.get(event.instance);
gs.getSession().impersonate('system');
var task = "[code]<a href='/" + current.sys_class_name + ".do?sys_id=" + current.sys_id + "'>" + current.number + "</a>[/code]";
var grReq = current.request_item.getRefRecord();
var count=0;
if (gr.next()) {
count = sc_task.getAggregate('count');
gs.log("Count of active task " + count);
}
if (count < 1) {
gs.log("No Child found");
//Found active child task
grReq.work_notes = gs.getMessage("Request Task {0} has been closed", task);
grReq.state = 2;
} else {
gs.log("Child found");
grReq.work_notes = gs.getMessage("Request Task {0} has been closed", task);
grReq.update();
}
}
Hope this helps.