Query on sibling task

tsoct
Tera Guru

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();
    }
}

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@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.

View solution in original post

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@tsoct Does this code reside in a script action? If yes then could you share the code which is triggering the event.

Yes. There is a before Update Business rule used to trigger the event. 

 

tsoct_0-1696695950915.png

 

(function executeRule(current, previous /*null when async*/ ) {

    gs.eventQueue('updateRITM.record.stateupdate', current); 

})(current, previous);

 

 

 

Sandeep Rajput
Tera Patron
Tera Patron

@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.