The CreatorCon Call for Content is officially open! Get started here.

GlideAggregate is not working

sudhir041
Tera Contributor

I have a script and currently it is not working with GlideAggregate can someone please rectify it:

 

var RITM = new GlideRecord("sc_req_item");
RITM.addEncodedQuery("state=2"); //state is in progress
RITM.query();
while (RITM.next()) {
    var p= 0;
    var Task = new GlideAggregate("sc_task");
    Task.addQuery("request_item", RITM.sys_id);
    Task.addAggregate('COUNT');
   Task.query();
    var total = Task.getAggregate('COUNT');
    while (Task.next()) {
        if (Task.active.toString() == 'false') {
            p++;
        }
    }

    if (p == total && total != 0) {
       gs.print(RITM.number);
    }
}
1 ACCEPTED SOLUTION

Here is a script that can do that. Depending on the instance it's going for, amount of RITMs, active tasks and so on it might need to be tweaked. Perhaps even run as a flow instead and so on. I would also probably throw in some more checks in case there aren't any RITMs with state "in progress" or active tasks.

var RITM = new GlideRecord("sc_req_item");
RITM.addEncodedQuery("state=2"); //state is in progress
RITM.query();

var ritmSysids = [];
//Get all RITM sys_ids and put them in a array
while (RITM.next()) {
ritmSysids.push(RITM.getUniqueValue());
}    

var checkTasks = new GlideRecord('sc_task');
checkTasks.addQuery('request_item','IN',ritmSysids);
checkTasks.addQuery('active',true);
checkTasks.query();

//Go through active tasks and remove the RITM from the array since it should only contain
//RITM with no active tasks
while(checkTasks.next()){

ritmSysids = ritmSysids.filter(function(item) {
return item != checkTasks.request_item
})
}

gs.info("Sys_ids of RITM with no active tasks: " + ritmSysids)

 

 

View solution in original post

16 REPLIES 16

Hi @sudhir041 ,

var ritmArr=[];
var RITM = new GlideRecord("sc_req_item");
RITM.addEncodedQuery("active=true");
RITM.query();
while (RITM.next()) 
{
var Task = new GlideRecord("sc_task");
Task.addQuery("request_item="+RITM.getUniqueValue()+"^active=true")
Task.query();
if (!Task.next()) 
{
ritmArr.push(RITM.getValue('number'))
}
}

gs.info(ritmArr)

 

This will give you all RITM without any active task.

 

 

 


Thanks and Regards,

Saurabh Gupta

Here is a script that can do that. Depending on the instance it's going for, amount of RITMs, active tasks and so on it might need to be tweaked. Perhaps even run as a flow instead and so on. I would also probably throw in some more checks in case there aren't any RITMs with state "in progress" or active tasks.

var RITM = new GlideRecord("sc_req_item");
RITM.addEncodedQuery("state=2"); //state is in progress
RITM.query();

var ritmSysids = [];
//Get all RITM sys_ids and put them in a array
while (RITM.next()) {
ritmSysids.push(RITM.getUniqueValue());
}    

var checkTasks = new GlideRecord('sc_task');
checkTasks.addQuery('request_item','IN',ritmSysids);
checkTasks.addQuery('active',true);
checkTasks.query();

//Go through active tasks and remove the RITM from the array since it should only contain
//RITM with no active tasks
while(checkTasks.next()){

ritmSysids = ritmSysids.filter(function(item) {
return item != checkTasks.request_item
})
}

gs.info("Sys_ids of RITM with no active tasks: " + ritmSysids)