GlideAggregate API and it's Methods for Scripting.
GlideAggregate API and it's Methods for Scripting.
Please note: The APIs below are intended for scoped applications and may behave differently in the
global scope.
GlideAggregate
GlideAggregate enables creating database aggregation queries.
The scoped GlideAggregate class is an extension of GlideRecord and provides database aggregation (AVG,
COUNT, MIN, MAX, STDDEV, SUM) queries. This functionality can be helpful when creating customized reports or
in calculations for calculated fields. The GlideAggregate class works only on number fields.
When you use GlideAggregate on currency or price fields, you are working with the reference currency value. Be
sure to convert the aggregate values to the user's session currency for display. Because the conversion rate
between the currency or price value (displayed value) and its reference currency
(https://docs.servicenow.com/bundle/quebec-platform-
administration/page/administer/currency/concept/currency.html) value (aggregation value) might change, the result
may not be what the user expects.
Note: When using an on-premise system, the database server time zone must be set to GMT/UTC for this class to
work properly.
GlideAggregate(String tableName)
Creates a GlideAggregate object on the specified table.
Parameters
Example
Parameters
AVG
COUNT
agg String
MIN
MAX
STDDEV
SUM
Optional. Name of the field to group the results of the aggregation by.
name String
Default: Null
Returns
Type Description
Example
The following shows how to add aggregates to a query on the category and software fields in the Incident
[incident] table.
incidentGA.addQuery('category', 'software');
incidentGA.setGroup(false);
incidentGA.addAggregate('COUNT', 'sys_mod_count');
incidentGA.addAggregate('SUM', 'sys_mod_count');
incidentGA.addAggregate('AVG', 'sys_mod_count');
incidentGA.addAggregate('MIN', 'sys_mod_count');
incidentGA.addAggregate('MAX', 'sys_mod_count');
incidentGA.addAggregate('STDDEV', 'sys_mod_count');
incidentGA.query();
if (incidentGA.next()) {
gs.info('COUNT: ' + incidentGA.getAggregate('COUNT', 'sys_mod_count'));
gs.info('SUM: ' + incidentGA.getAggregate('SUM', 'sys_mod_count'));
gs.info('AVG: ' + incidentGA.getAggregate('AVG', 'sys_mod_count'));
gs.info('MIN: ' + incidentGA.getAggregate('MIN', 'sys_mod_count'));
gs.info('MAX: ' + incidentGA.getAggregate('MAX', 'sys_mod_count'));
gs.info('STDDEV: ' + incidentGA.getAggregate('STDDEV', 'sys_mod_count'));
}
COUNT: 13
SUM: 273
AVG: 21.0000
MIN: 3
MAX: 95
STDDEV: 32.7694
addEncodedQuery(String query)
Adds an encoded query to the other queries that may have been set for this aggregate.
Parameters
Returns
Type Description
Example
addNotNullQuery(String fieldName)
Adds a not null query to the aggregate.
Parameters
Returns
Type Description
Example
addNullQuery(String fieldName)
Adds a null query to the aggregate.
Parameters
Returns
Type Description
Example
Parameters
Name Type Description
Returns
Type Description
Example
Parameters
Name Type Description
fieldName String Name of the field for which trending should occur.
date
dayofweek
hour
timeInterval String
minute
quarter
value
week
year
Returns
Type Description
Example
3/2018, 9
4/2018, 2
1/2019, 38
2/2019, 310
Parameters
Name Type Description
Type of aggregate.
Valid values:
AVG
COUNT
agg String
MIN
MAX
STDDEV
SUM
Returns
Type Description
Example
This shows a COUNT aggregation that returns the number of records in the Incident table.
Number of incidents: 63
Example
getAggregateEncodedQuery()
Gets the query necessary to return the current aggregate.
Returns
Type Description
Example
category=database
category=hardware
category=inquiry
category=network
category=request
category=software
getEncodedQuery()
Retrieves the encoded query.
Returns
Type Description
Example
var count = new GlideAggregate('incident');
count.addAggregate('MIN', 'sys_mod_count');
count.addAggregate('MAX', 'sys_mod_count');
count.addAggregate('AVG', 'sys_mod_count');
count.groupBy('category');
count.query();
gs.info(count.getEncodedQuery());
ORDERBYcategory^GROUPBYcategory
getRowCount()
Retrieves the number of rows in the GlideAggregate object.
Returns
Type Description
Example
6
Database Update counts: MIN = 8 MAX = 48 AVG = 28.0000
Hardware Update counts: MIN = 4 MAX = 14 AVG = 6.6250
Inquiry / Help Update counts: MIN = 0 MAX = 34 AVG = 6.5714
Network Update counts: MIN = 3 MAX = 37 AVG = 18.6000
Request Update counts: MIN = 5 MAX = 39 AVG = 13.4000
Software Update counts: MIN = 4 MAX = 98 AVG = 24.0000
getTableName()
Retrieves the table name associated with this GlideAggregate object.
Returns
Type Description
Example
getValue(String name)
Returns the value of the specified field.
Parameters
name String Name of the field within the current table to return.
Returns
Type Description
Example
groupBy(String name)
Provides the name of a field to use in grouping the aggregates.
Parameters
Returns
Type Description
Example
hasNext()
Determines if there are any more records in the GlideAggregate object.
Returns
Type Description
Example
var agg = new GlideAggregate('incident');
agg.addAggregate('AVG', 'sys_mod_count');
agg.groupBy('category');
agg.query();
while (agg.hasNext()) {
agg.next();
var avg = agg.getAggregate('AVG', 'sys_mod_count');
var category = agg.category.getDisplayValue();
gs.info(category + ': AVG = ' + avg);
}
next()
Moves to the next record in the GlideAggregate.
Returns
Type Description
Boolean True if there are more records in the query set; otherwise, false.
Example
orderBy(String name)
Orders the aggregates using the value of the specified field. The field will also be added to the group-by list.
Parameters
Returns
Type Description
Example
Parameters
Returns
Type Description
Example
ga.addAggregate(‘COUNT’, ‘category’);
ga.orderByAggregate('count', 'category');
ga.query();
while(ga.next()) {
gs.info(‘Category ’ + ga.category + ‘ ‘ + ga.getAggregate(‘COUNT’, ‘category’));
}
Category inquiry 18
Category software 11
Category hardware 7
Category network 5
Category request 5
Category 4
Category database 2
orderByDesc(String name)
Sorts the aggregates in descending order based on the specified field. The field will also be added to the group-
by list.
Parameters
Returns
Type Description
Example
query()
Issues the query and gets the results.
Returns
Type Description
Example
setGroup(Boolean b)
Sets whether the results are to be grouped.
Parameters
Returns
Type Description
Example
var ga = new GlideAggregate('incident');
ga.addAggregate('COUNT', 'category');
ga.setGroup(true);
ga.groupBy("category");
ga.query();
while(ga.next()) {
gs.info('Category ' + ga.category + ' ' + ga.getAggregate('COUNT', 'category'));
}
Category database 2
Category hardware 7
Category inquiry 18
Category network 5
Category request 5
Category software 11