GlideQuery aggregate count null

Colleen
Tera Expert

Why is this GlideQuery returning count: null ?

 

var example = new GlideQuery('incident')
    .where('sys_created_on', '>=', new GlideDateTime('2025-01-01 00:00:00'))
    .groupBy('priority')
	.aggregate('count', 'number')
    .having('count', 'number', '>', 1)
    .select().toArray(100);

gs.print(JSON.stringify(example));

 

Result:

 [{"group":{"priority":0},"count":null},{"group":{"priority":1},"count":null},{"group":{"priority":2},"count":null},{"group":{"priority":3},"count":null},{"group":{"priority":4},"count":null}]

 

3 REPLIES 3

Tushar
Kilo Sage
Kilo Sage

Hi @Colleen 

 
using aggregate('count', field), GlideQuery expects the field (e.g., number) to be a valid field for counting non-null values. However, in ServiceNow, count aggregation in GlideQuery does not work the same way as in a traditional SQL COUNT(field).

please try this - 

var example = new GlideQuery('incident')
.where('sys_created_on', '>=', new GlideDateTime('2025-01-01 00:00:00'))
.groupBy('priority')
.aggregate('sum', 'reassignment_count')
.aggregate('count') // remove 'number' to count total records in each group
.having('sum', 'reassignment_count', '>', 4)
.select()
.toArray(100);

gs.print(JSON.stringify(example));

 

Thanks.

Colleen
Tera Expert

Hi Tushar

 

Thanks for the prompt response. Apologies, my example script was not clear.  I updated the script after posting, but you probably replied to the original script.

 

I want to base the having clause on the count. My understanding is that having needs to use the same field as the aggregate. 

 

The results appear to return the correct information based on the having clause, even though the count is null.

Ankur Bawiskar
Tera Patron
Tera Patron

@Colleen 

check this link

GlideQuery Cheat Sheet 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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