Counting records with GlideAggregate
Earlier in this chapter, the getRowCount
function of GlideRecord
was introduced. It returns the number of results found. However, this is only determined by getting all the information from the database and then counting it. Wouldn't it be more efficient if we could get just the total number? We can, with GlideAggregate
!
Note
The developer site has more information available: https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&type=server&scoped=true&to=class__scoped_glideaggregate__helsink
.
Run the following lines of code to get the total number of records that were created yesterday:
var today = new GlideDate(); var yesterdayTime = new GlideDateTime(); yesterdayTime.addDaysUTC(-1); var yesterday = new GlideDate(); yesterday.setValue(yesterdayTime); var count = new GlideAggregate('x_hotel_check_in'); count.addQuery('sys_created_on', '<', today); count.addQuery('sys_created_on', '>=', yesterday); count.addAggregate...