Trying to use GlideAggregate to count only specific catalog items

mrcorbeaux
Kilo Guru

The below workflow Run Script is using GlideAggregate to count how many RITMs are included in an REQ and adds that number to the correlation_id field of the REQ. I am trying to update it so that it only adds to the correlation_id field if specific catalog items are requested while ignoring others on the same REQ. I tried a number of things including the part I commented out which was an attempt to only count specific catalog item sys_ids, but no luck.
Any suggestions?

var agg = new GlideAggregate('sc_req_item');
agg.addQuery('request', current.sys_id);
//agg.addQuery('sys_id', '566ff7371ba2601064c8a686624bcb98');
agg.addAggregate("COUNT");
agg.query();
if(agg.next()){
var counter = agg.getAggregate("COUNT");
var req_update = new GlideRecord('sc_request');
req_update.get(current.sys_id);
req_update.correlation_id = counter;
req_update.update();
}

vkachineni
Mega Sage
//This worked on PDI

var agg = new GlideAggregate('sc_req_item');
agg.addQuery('request', "c3b22a00db323300868370e2399619f0"); //REQ sys_id
agg.addAggregate("COUNT");
agg.query();
if(agg.next()){
    var counter = agg.getAggregate("COUNT");
    gs.log("counter = " + counter, '@@@TESTING');
    var req_update = new GlideRecord('sc_request');
    req_update.get("c3b22a00db323300868370e2399619f0"); //Load the same REQ
    req_update.setValue('correlation_id',counter); // this is the only change made
    var id = req_update.update();
    if(id){
        gs.log("Updated = " + req_update.number, '@@@TESTING');
    }
}
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Thanks but unfortunately that did not work. I replaced c3b22a00db323300868370e2399619f0 with one of my sys_ids but REQ's CorrID did not count it.

//Just to make sure, the sys_id you used is of a REQ...right?

//Can you check with try catch{}. Please check the log and see if an exception is logged

var agg = new GlideAggregate('sc_req_item');
agg.addQuery('request', "c3b22a00db323300868370e2399619f0"); //REQ sys_id
agg.addAggregate("COUNT");
agg.query();
if(agg.next()){
    var counter = agg.getAggregate("COUNT");
    gs.log("counter = " + counter, '@@@TESTING');
    var req_update = new GlideRecord('sc_request');
    req_update.get("c3b22a00db323300868370e2399619f0"); //Load the same REQ
    req_update.setValue('correlation_id',counter); // this is the only change made
	try{
		var id = req_update.update();
		if(id){
			gs.log("Updated = " + req_update.number, '@@@TESTING');
		} else{
			gs.log("Not Updated = " + req_update.number, '@@@TESTING');
		}
	}
	catch (ex) 
	{
		var message = ex.getMessage();
        gs.log("Error: " + message, '@@@TESTING'); //Check the log				
    }    
}				

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Vkachineni,

The sysID is actually of a specific catalog item and not the REQ. My original code was misleading.

Thank you.