How to check if GlideAggregate query doesn't return any records

nmsmtys
Tera Contributor

Hi All,
I'm pulling data from a table using GlideAggregate, and I'd like to implement "error handling", meaning that I'd like to log a message when the GA query did not return any data. Initially, I implemented it like this (not exact code, just the main logic):

var ga = new GlideAggregate(tableName);

ga.addAggregate('SUM', 'field_name');

ga.addQuery('year', year);

ga.addQuery('month', month)

ga.query();

 

if (ga.next() {

// getAggregate

} else {

gs.warn('missing data from table');

}

The problem with this is that if you add an aggregate to GA, it will always return an object, even it is empty, and therefore will always run into the if clause. My other idea was to check if the aggregate returned is an empty string, but I have tables where I have to aggregate 8-10 field values, and each of them would have to be checked individually. My question would be that how could I check if the GA query actually didn't return any records?

 

Many thanks

2 REPLIES 2

SuhasPSalunkhe
Kilo Guru

You can use aggregate function COUNT.

This way you can have at least count.

May be that can help you

reshmapatil
Tera Guru

If you want to get any field value by using GlideAggregate best is to use that field somewhere in the query.

 

For example: I want to get Assigned to value

var ga = new GlideAggregate(tableName);

ga.addAggregate('SUM', 'field_name');

ga.addEncodedQuery('year', +year+'^month', +month+'^assigned_to!=NULL');

ga.query();

 

if (ga.next() {

// getAggregate

var ass = assigned_to;

} else {

gs.warn('missing data from table');

}

 

Regards,

Reshma

**Please mark my answer correct or helpful based on the impact**