The GlideAggregate API is a powerful tool in ServiceNow that allows developers to perform complex calculations and aggregate functions on records in a table. With the GlideAggregate API, developers can perform functions such as counting records, finding the maximum or minimum value of a field, or summing up the values of a field for a particular group of records.
In this section, we'll explore the GlideAggregate API and its various methods, along with examples of how to use them.
Creating a GlideAggregate Object To get started with the GlideAggregate API, we first need to create a GlideAggregate object that represents the table we want to perform calculations on. The constructor of the GlideAggregate object takes the name of the table as an argument.
// Create a GlideAggregate object for the Incident table
var ga = new GlideAggregate('incident');
Once we have the GlideAggregate object, we can perform a variety of operations on it using the available methods.
Adding Aggregations The GlideAggregate API provides several methods for adding aggregations to a query, including addAggregate(), addEncodedQuery(), and groupBy(). These methods allow you to perform calculations and aggregate functions on records in the table based on certain conditions.
// Count the number of records in the Incident table with a priority of 1
var ga = new GlideAggregate('incident');
ga.addQuery('priority', '1');
ga.addAggregate('COUNT');
ga.query();
if (ga.next()) {
gs.log(ga.getValue('COUNT'));
}
In this example, we're counting the number of records in the Incident table with a priority of 1. We use the addQuery() method to specify the query condition, add the COUNT aggregation using the addAggregate() method, and then call the query() method to execute the query. Finally, we retrieve the count value using the getValue() method and log it.
Grouping Aggregations The GlideAggregate API also allows you to group aggregations based on certain fields in the table using the groupBy() method.
// Sum the value of the Impact field in the Incident table, grouped by Category
var ga = new GlideAggregate('incident');
ga.addAggregate('SUM', 'impact');
ga.groupBy('category');
ga.query();
while (ga.next()) {
gs.log(ga.category + ' - ' + ga.getValue('SUM', 'impact'));
}
In this example, we're summing up the value of the Impact field in the Incident table, grouped by Category. We use the addAggregate() method to add the SUM aggregation for the Impact field, use the groupBy() method to group the results by the Category field, and then loop through the result set using the next() method and log the category and sum value for each group.
Adding Conditions The GlideAggregate API also allows you to add conditions to aggregations using the addHaving() method.
// Find the maximum value of the Severity field in the Incident table for records with a priority of 1
var ga = new GlideAggregate('incident');
ga.addQuery('priority', '1');
ga.addAggregate('MAX', 'severity');
ga.addHaving('severity', '>', '3');
ga.query();
if (ga.next()) {
gs.log(ga.getValue('MAX', 'severity'));
}
In this example, we're finding the maximum value of the Severity field in the Incident table for records with a priority of 1. We use the addQuery() method to specify the query condition, add the MAX aggregation for the Severity field using the addAggregate() method, and then add a condition to the aggregation using the addHaving() method. Finally, we retrieve the maximum value using the getValue() method and log it.