How to get sys_id of a record using GlideAggregate

Kalyan Gadde
Giga Contributor

Hi,

My requirement is I need to count number of incidents with some assignment group and if there is only one incident with that perticular assignment group then I require that incident's sys_id . I don't want to use getRowCount(). I want to use only GlideAggregate. I have used the below script in my PDI but I did not get what I want.

Can Anybody help me on this.

var count = new GlideAggregate('incident');
count.addQuery('assignment_group','36c741fa731313005754660c4cf6a70d');  // Only one incident is there with this group 
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if (count.next())
incidents = count.getAggregate('COUNT');
gs.info(incidents);       // 1
gs.info(count.getUniqueValue());    // null
gs.info(count.getValue('sys_id'));   // no result
gs.info(count.sys_id.toString());   // no result

1 ACCEPTED SOLUTION

Well doing some more play around I was able to get the sys_id. Please find the code below:

var count = new GlideAggregate('incident');
count.addQuery('assignment_group', '36c741fa731313005754660c4cf6a70d'); // Only one incident is there with this group 
count.addAggregate('COUNT', 'sys_id');
count.query();
var incidents = 0;
if (count.next())
    incidents = count.getAggregate('COUNT', 'sys_id');
gs.info(incidents); // 1
if(incidents==1) //validate if there is only one incident
gs.info('Sys_ID : ' + count.sys_id.toString());

View solution in original post

9 REPLIES 9

Sai Kumar B
Mega Sage
Mega Sage

Hi @Kalyan Gadde 

Try this 


var count = new GlideAggregate('incident');
count.addQuery('assignment_group','36c741fa731313005754660c4cf6a70d');  // Only one incident is there with this group 
count.addAggregate('COUNT');
count.query();
if (count.next()) {
var incidents = count.getAggregate('COUNT');
gs.info(incidents);       // 1
gs.info(count.getUniqueValue()); 

}

AnirudhKumar
Mega Sage
Mega Sage

It's not possible to get Sys id using Glideaggregate.

Use GlideRecord class instead

Sai Kumar B
Mega Sage
Mega Sage

Hi @Kalyan Gadde 

Try this 

var ga = new GlideAggregate('incident');
ga.addEncodedQuery('assignment_group=36c741fa731313005754660c4cf6a70d');
ga.query();
if(ga.next()){
gs.info(ga.sys_id);
}

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you cannot get the record sys_id using GlideAggregate

you will have to do 1 more query

var count = new GlideAggregate('incident');
count.addQuery('assignment_group','36c741fa731313005754660c4cf6a70d');  // Only one incident is there with this group 
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if (count.next()){
	incidents = count.getAggregate('COUNT');
	gs.info(incidents);       // 1

	var inc = new GlideRecord('incident');
	inc.addQuery('assignment_group', count.assignment_group);
	inc.query();
	if(inc.next()){
		gs.info('INC sys_id' + inc.sys_id);
	}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader