GlideAggregate - Scoped
-
- UpdatedJan 30, 2025
- 11 minutes to read
- Yokohama
- API reference
The GlideAggregate API enables creating database aggregation queries.
The GlideAggregate class is an extension of the GlideRecord class and provides database aggregation (AVG, COUNT, GROUP_CONCAT, GROUP_CONCAT_DISTINCT, MAX, MIN, STDDEV, SUM) queries. This functionality can be helpful when creating customized reports or in calculations for calculated fields.
When you use GlideAggregate methods 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 value (aggregation value) might change, the result may not be what the user expects.
Scoped GlideAggregate - GlideAggregate(String tableName)
Creates a GlideAggregate object on the specified table.
Name | Type | Description |
---|---|---|
tableName | String | Name of the table. |
Example
Scoped GlideAggregate - addAggregate(String agg, String name)
Adds an aggregate to a database query.
Name | Type | Description |
---|---|---|
agg | String | Name of an aggregate to use. Valid values:
|
name | String | Optional. Name of the field to group the results of the aggregation
by. Default: Null |
Type | Description |
---|---|
None |
Example
The following example shows how to use GlideAggregate functions on the Incident [incident] table.
Output.
Scoped GlideAggregate - addEncodedQuery(String query)
Adds an encoded query to the other queries that may have been set for this aggregate.
Name | Type | Description |
---|---|---|
query | String | An encoded query to add to the aggregate. |
Type | Description |
---|---|
None |
Example
Scoped GlideAggregate - addQuery(String name, String operator, String value)
Adds a query to the aggregate.
Name | Type | Description |
---|---|---|
name | String | Query to add. |
operator | String | Operator for the query. |
value | String | List of values to include in the query. |
Type | Description |
---|---|
GlideQueryCondition | Query condition. |
Example
Scoped GlideAggregate - addNotNullQuery(String fieldName)
Adds a not null query to the aggregate.
Name | Type | Description |
---|---|---|
fieldname | String | Name of the field. |
Type | Description |
---|---|
GlideQueryCondition | Scoped query condition. |
Example
Scoped GlideAggregate - addNullQuery(String fieldName)
Adds a null query to the aggregate.
Name | Type | Description |
---|---|---|
fieldName | String | Name of the field. |
Type | Description |
---|---|
GlideQueryCondition | The scoped query condition. |
Example
Scoped GlideAggregate - addTrend(String fieldName, String timeInterval, Number numUnits)
Adds a trend for a specified field. Use a trend to show patterns over a period of time.
Name | Type | Description |
---|---|---|
fieldName | String | Name of the field for which trending should occur. |
timeInterval | String | Time interval for the trend. Valid values:
|
numUnits | Number | Optional. Only valid when timeInterval =
minute. Number of minutes to include in the trend. Default: 1 |
Type | Description |
---|---|
void |
Example
Scoped GlideAggregate - getAggregate(String agg, String name)
Returns the value of an aggregate from the current record.
Name | Type | Description |
---|---|---|
agg | String | Type of the aggregate. Valid values:
|
name | String | Name of the field on which to perform the aggregation. |
Type | Description |
---|---|
String | The value of the aggregation. If the values being aggregated are FX Currency
values, the returned value is in the format
<currency_code;currency_value> , such as:
USD;134.980000. Note: If the specified field
contains FX Currency values of mixed currency types, the method is not able to
aggregate the values and returns a semicolon (;). |
Example
Shows an aggregation that returns the number of records in the Incident table.
Output: Number of incidents: 63.
Example
Shows the aggregation of an FX Currency field.
Output:
Scoped GlideAggregate - getAggregateEncodedQuery()
Gets the query necessary to return the current aggregate.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
String | Encoded query to get the aggregate. |
Example
Scoped GlideAggregate - getEncodedQuery()
Retrieves the encoded query.
Name | Type | Description |
---|---|---|
none |
Type | Description |
---|---|
String | Encoded query. |
Example
Scoped GlideAggregate - getRowCount()
Retrieves the number of rows in the GlideAggregate object.
Name | Type | Description |
---|---|---|
none |
Type | Description |
---|---|
Number | Number of rows in the GlideAggregate object. |
Example
Scoped GlideAggregate - getTableName()
Retrieves the table name associated with this GlideAggregate object.
Name | Type | Description |
---|---|---|
none |
Type | Description |
---|---|
String | Table name. |
Example
Scoped GlideAggregate - getValue(String name)
Returns the value of the specified field.
Name | Type | Description |
---|---|---|
name | String | Name of the field within the current table to return. |
Type | Description |
---|---|
String | Value of the specified field. Returns null if invalid (not part of result set). |
Example
Output:
Scoped GlideAggregate - groupBy(String name)
Provides the name of a field to use in grouping the aggregates.
May be called numerous times to set multiple group fields.
Name | Type | Description |
---|---|---|
name | String | Name of the field. |
Type | Description |
---|---|
None |
Example
Scoped GlideAggregate - hasNext()
Determines if there are any more records in the GlideAggregate object.
Name | Type | Description |
---|---|---|
none |
Type | Description |
---|---|
Boolean | Flag that indicates whether there are more results in the query set. Possible values:
|
Example
Scoped GlideAggregate - next()
Moves to the next record in the GlideAggregate.
Name | Type | Description |
---|---|---|
none |
Type | Description |
---|---|
Boolean | Flag that indicates whether there are more results in the query set. Possible values:
|
Example
Scoped GlideAggregate - orderBy(String name)
Orders the aggregates using the value of the specified field. The field is also added to the group-by list.
Name | Type | Description |
---|---|---|
name | String | Name of the field to order the aggregates by. Alternatively, you can provide a glidefunction to order the aggregates, such as |
Type | Description |
---|---|
None |
Example
Scoped GlideAggregate - orderByAggregate(String agg, String fieldName)
Orders the aggregates based on the specified aggregate and field.
Name | Type | Description |
---|---|---|
agg | String | Type of aggregation. |
fieldName | String | Name of the field to aggregate. |
Type | Description |
---|---|
void |
Example
Output:
Scoped GlideAggregate - 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.
Name | Type | Description |
---|---|---|
name | String | Name of the field. |
Type | Description |
---|---|
None |
Example
Scoped GlideAggregate - query()
Issues the query and gets the results.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
None |
Example
Scoped GlideAggregate - setAggregateWindow(Number firstRow, Number lastRow)
Limits the number of rows from the table to include in the aggregate query.
Name | Type | Description |
---|---|---|
firstRow | Number | Zero-based index of the first row to include in the aggregate query, inclusive. |
lastRow | Number | Zero-based index of the last row to include in the aggregate query, exclusive. |
Type | Description |
---|---|
None |
Example
Prints the count of each category for the first ten records in the Incident [incident] table.
var incidentGroup = new GlideAggregate('incident');
incidentGroup.addAggregate('COUNT', 'category');
incidentGroup.setAggregateWindow(0, 10);
incidentGroup.query();
while (incidentGroup.next()) {
var incidentCount = incidentGroup.getAggregate('COUNT', 'category');
gs.info('{0} count: {1}', [incidentGroup.getValue('category'), incidentCount]);
}
Output:
database count: 1
Hardware count: 1
inquiry count: 7
software count: 1
Scoped GlideAggregate - setGroup(Boolean b)
Sets whether to group the return results.
Name | Type | Description |
---|---|---|
b | Boolean | Flag that indicates whether to group the results. Valid values:
|
Type | Description |
---|---|
void |
Example
Output:
Scoped GlideAggregate - setIntervalYearIncluded(Boolean b)
Sets whether to group results by year for day-of-week trends. These trends are created using the addTrend() method with the dayofweek time interval.
Dependency: Scoped GlideAggregate - addTrend('<fieldName>', 'dayofweek').
Name | Type | Description |
---|---|---|
b | Boolean | Flag that indicates whether to include a year for a trend with a day-of-week time interval. Valid values:
Default: true |
Type | Description |
---|---|
None |
Example
The following example shows how to count incidents created in the last six months. The incidents are separated by the day of the week, but not including the year. For example, the default results for Thursday would include the
year, such as Thursday/2023: 16
.
Output:
On this page
- Scoped GlideAggregate - GlideAggregate(String tableName)
- Scoped GlideAggregate - addAggregate(String agg, String name)
- Scoped GlideAggregate - addEncodedQuery(String query)
- Scoped GlideAggregate - addQuery(String name, String operator, String value)
- Scoped GlideAggregate - addNotNullQuery(String fieldName)
- Scoped GlideAggregate - addNullQuery(String fieldName)
- Scoped GlideAggregate - addTrend(String fieldName, String timeInterval, Number
numUnits)
- Scoped GlideAggregate - getAggregate(String agg, String name)
- Scoped GlideAggregate - getAggregateEncodedQuery()
- Scoped GlideAggregate - getEncodedQuery()
- Scoped GlideAggregate - getRowCount()
- Scoped GlideAggregate - getTableName()
- Scoped GlideAggregate - getValue(String name)
- Scoped GlideAggregate - groupBy(String name)
- Scoped GlideAggregate - hasNext()
- Scoped GlideAggregate - next()
- Scoped GlideAggregate - orderBy(String name)
- Scoped GlideAggregate - orderByAggregate(String agg, String fieldName)
- Scoped GlideAggregate - orderByDesc(String name)
- Scoped GlideAggregate - query()
- Scoped GlideAggregate - setAggregateWindow(Number firstRow, Number lastRow)
- Scoped GlideAggregate - setGroup(Boolean b)
- Scoped GlideAggregate - setIntervalYearIncluded(Boolean b)