How to get list of RITM which doesn't have any associated Ctasks.

Vijay Baokar
Kilo Sage

Hello Folks,

 

I am trying to get list of RITMs which are opened 90 days ago, active true, approval is not waiting for approval and those RITMs doesn't have any Ctask associated with them.

 

(function() {
var ritmGr = new GlideRecord('sc_req_item');
ritmGr.addEncodedQuery('stage!=waiting_for_approval^active=true^opened_atRELATIVELT@dayofweek@ago@90');
ritmGr.query();
 
while (ritmGr.next()) {
var Ctask = new GlideRecord('sc_task');
Ctask.addQuery('request_item', ritmGr.sys_id);
Ctask.query();
while(Ctask.next())
 
gs.info('Ctask Number: ' + Ctask.number);
}
})();
 
but this will give me list of Ctask which are created against RITM but i need list of RITMs which doesn't have any Ctask associated with them.
6 REPLIES 6

Adrian Ubeda
Mega Sage
Mega Sage

Hi @Vijay Baokar , 

Instead doing two glideRecords I would use glideAggregate for counting in the second one, check this about GlideAggregate: https://developer.servicenow.com/blog.do?p=/post/glideaggregate/

And, your code should look something as follows:

(function() {
    var ritmGr = new GlideRecord('sc_req_item');
    ritmGr.addEncodedQuery('stage!=waiting_for_approval^active=true^opened_atRELATIVELT@dayofweek@ago@90');
    ritmGr.query();

    while (ritmGr.next()) {
        var Ctask = new GlideAggregate('sc_task');
		Ctask.addAggregate('COUNT', 'request_item');
        Ctask.addQuery('request_item', ritmGr.sys_id);
        Ctask.query();
        
		if(Ctask.getAggregate('COUNT') == 0){
			gs.info('Ctask : ' + Ctask.getDisplayValue());
		}
})();
If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆

dgarad
Giga Sage

Hi @Vijay Baokar 

 try the below code.

var noTaskCounter = 0;
var ritmGr = new GlideRecord('sc_req_item');
ritmGr.addEncodedQuery('stage!=waiting_for_approval^active=true^opened_atRELATIVELT@dayofweek@ago@90');
ritmGr.query();
 
while (ritmGr.next()) {
var Ctask = new GlideRecord('sc_task');
Ctask.addQuery('request_item', ritmGr.sys_id);
Ctask.query();
if(!Ctask.next()){
 
gs.info('Ctask Number: ' + ritmGr.number);
 noTaskCounter++;
}
}
If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

@dgarad can we get the chunk of 100 record at a time ? i put the setLimit (100); but didn't work.

where do you apply setLimit?

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad