GlideAggregate on Remote Table

Colleen
Tera Expert

I've created a remote table that queries an external data source, and I want a count of the records that satisfy a specific query.

  

This script returns null:

var sections = new GlideAggregate('x_uob15_canvas_adm_st_course_section');
sections.addQuery('course.sis_id', 'C2023-B08835').addCondition('start_at', '>', '2024-09-01 00:00:00');
sections.addAggregate('COUNT');
sections.query();

gs.print(sections.getAggregate('COUNT'));

This script returns NaN:

var sections = new global.GlideQuery('x_uob15_canvas_adm_st_course_section')
.where('course.sis_id', 'C2023-B08835').where('start_at', '>', '2024-09-01 00:00:00')
.count();

gs.print(sections);

 

However, these scripts return the expected result:

var sections = new GlideRecord('x_uob15_canvas_adm_st_course_section');
sections.addQuery('course.sis_id', 'C2023-B08835').addCondition('start_at', '>', '2024-09-01 00:00:00');
sections.query();

gs.print(sections.getRowCount());

OR

var sections = new global.GlideQuery('x_uob15_canvas_adm_st_course_section')
.where('course.sis_id', 'C2023-B08835').where('start_at', '>', '2024-09-01 00:00:00')
.select().toArray(100).length;

gs.print(sections);

 

I cannot find documentation on aggregate methods in the v_query API.

 

Has anyone successfully used GlideAggregate on a remote table?

1 ACCEPTED SOLUTION

Ravi Gaurav
Giga Sage
Giga Sage

Hi @Colleen 

GlideAggregate (and GlideQuery’s .count()) don’t work on Remote Tables. Remote Tables only implement a subset of query operations; aggregate functions aren’t supported, so you get null/NaN. Your two “working” snippets succeed because they materialize rows (or at least a page of them) and then count locally.

var q = new global.GlideQuery('x_uob15_canvas_adm_st_course_section')

var count = q.toArray(1000).length;
gs.print(count);

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

8 REPLIES 8

@Colleen 

 

I created a scoped app to test this and it is working fine.

 

Check your query & value passed to the reference field. Change the reference field value as below to use sys id of the record [requested_by is a reference field in my example]

 

Bhuvan_4-1757405402236.png

 

Bhuvan_2-1757405363854.pngBhuvan_3-1757405377788.png

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

@Colleen 

 

You can also use the query as below. Check the field in scoped app table that is reference to another table and check the field name in referenced table used in your query.

 

For example, requested_by field in scoped app table is reference to sys_user table and field name in the referenced table is 'name'. You can also use only 'requested_by' in the query if you can use sys_id of the record as referenced values are stored with sys_id in the record

Bhuvan_2-1757407245266.png

var loaned_books= new global.GlideQuery('x_1123125_loaner_0_loaner_request')
.where('active', true)
.where('category','books')
.where('sys_created_by','>','2025-09-09 00:00:00')
.where('requested_by.name','Bhuvan Ekanathan')
.count()
gs.info('Loaned Books: ' + loaned_books);

Bhuvan_0-1757406911094.png

Bhuvan_1-1757406964636.png

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

@Colleen 

 

Did you get a chance to review this as I believe the information provided should answer your question.

 

As per community guidelines, you can accept more than one answer as accepted solution. If my response helped to guide you or answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

Ravi Gaurav
Giga Sage
Giga Sage

Hi @Colleen 

GlideAggregate (and GlideQuery’s .count()) don’t work on Remote Tables. Remote Tables only implement a subset of query operations; aggregate functions aren’t supported, so you get null/NaN. Your two “working” snippets succeed because they materialize rows (or at least a page of them) and then count locally.

var q = new global.GlideQuery('x_uob15_canvas_adm_st_course_section')

var count = q.toArray(1000).length;
gs.print(count);

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/