Skip to content

Commit b56ae96

Browse files
authored
SimpleGlideAggregate Utility (#2252)
* Implement SimpleGlideAggregate class for aggregations * Add SimpleGlideAggregate utility with usage examples Introduced SimpleGlideAggregate utility to simplify and enhance GlideAggregate usage in ServiceNow. Provides a chainable API for common aggregation operations and includes sample usage. * Add SimpleGlideAggregate.js to GlideAggregate folder * Move file to GlideAggregate folder * Rename SimpleGlideAggregate.js to Readme.md * Move files to the appropriate folder * Move file to appropriate folder * Add API Token Expiry Warning script This script checks for OAuth tokens nearing expiry and sends a warning email to specified recipients. * Add Readme for API Token Expiry Warning script Added documentation for the API Token Expiry Warning script, detailing its purpose and benefits. * Delete Server-Side Components/Scheduled Jobs/API Token Expiry Warning directory
1 parent bb6c6ae commit b56ae96

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
SimpleGlideAggregate Utility
2+
**Overview**
3+
SimpleGlideAggregate is a developer utility Script Include for ServiceNow that provides a simplified, chainable API around the native GlideAggregate class. It abstracts complexities of writing aggregation queries and returns results in an easy-to-use JavaScript object format.
4+
Because OOTB glideAggregate API is little bit different so I tried to create a new function with a simper version.
5+
**Purpose**
6+
Simplify aggregate queries such as COUNT, SUM, MIN, and MAX for developers, especially those less familiar with GlideAggregate methods.
7+
Provide an intuitive interface for common aggregation operations with chaining support.
8+
Facilitate viewing aggregate results alongside individual records matching the same criteria for better analysis.
9+
10+
**Sample Usage of the functions :**
11+
var sga = new SimpleGlideAggregate('incident');
12+
13+
// Build query and add all supported aggregates
14+
var results = sga
15+
.addQuery('active', true) // Filter: active incidents only
16+
.addQuery('priority', '>=', 2) // Priority 2 or higher
17+
.count() // Count matching records
18+
.sum('duration') // Sum of duration field instead of impact
19+
.min('priority') // Minimum priority value in results
20+
.max('sys_updated_on') // Most recent update timestamp
21+
.execute();
22+
23+
gs.info('Aggregate Results:');
24+
gs.info('Count: ' + results.COUNT);
25+
gs.info('Sum of Duration: ' + (results.SUM_duration !== undefined ? results.SUM_duration : 'N/A'));
26+
gs.info('Minimum Priority: ' + (results.MIN_priority !== undefined ? results.MIN_priority : 'N/A'));
27+
gs.info('Most Recent Update (max sys_updated_on timestamp): ' + (results.MAX_sys_updated_on !== undefined ? results.MAX_sys_updated_on : 'N/A'));
28+
29+
// Optionally fetch some matching record details to complement the aggregate data
30+
var gr = new GlideRecord('incident');
31+
gr.addQuery('active', true);
32+
gr.addQuery('priority', '>=', 2);
33+
gr.orderByDesc('sys_updated_on');
34+
gr.setLimit(5);
35+
gr.query();
36+
37+
gs.info('Sample Matching Incidents:');
38+
while (gr.next()) {
39+
gs.info('Number: ' + gr.getValue('number') + ', Priority: ' + gr.getValue('priority') + ', Updated: ' + gr.getValue('sys_updated_on'));
40+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
var SimpleGlideAggregate = Class.create();
2+
SimpleGlideAggregate.prototype = {
3+
initialize: function(tableName) {
4+
if (!tableName) {
5+
throw new Error("Table name is required.");
6+
}
7+
this._table = tableName;
8+
this._ga = new GlideAggregate(tableName);
9+
this._fields = [];
10+
this._conditionsAdded = false;
11+
},
12+
13+
/**
14+
* Adds a query condition.
15+
* Usage: addQuery('priority', '=', '1') or addQuery('active', true)
16+
*/
17+
addQuery: function(field, operator, value) {
18+
if (value === undefined) {
19+
this._ga.addQuery(field, operator);
20+
} else {
21+
this._ga.addQuery(field, operator, value);
22+
}
23+
this._conditionsAdded = true;
24+
return this;
25+
},
26+
27+
/**
28+
* Adds COUNT aggregate.
29+
*/
30+
count: function() {
31+
this._fields.push({type: 'COUNT', field: null});
32+
return this;
33+
},
34+
35+
/**
36+
* Adds SUM aggregate on a field.
37+
*/
38+
sum: function(field) {
39+
if (!field) throw new Error("Field name required for sum.");
40+
this._fields.push({type: 'SUM', field: field});
41+
return this;
42+
},
43+
44+
/**
45+
* Adds MIN aggregate on a field.
46+
*/
47+
min: function(field) {
48+
if (!field) throw new Error("Field name required for min.");
49+
this._fields.push({type: 'MIN', field: field});
50+
return this;
51+
},
52+
53+
/**
54+
* Adds MAX aggregate on a field.
55+
*/
56+
max: function(field) {
57+
if (!field) throw new Error("Field name required for max.");
58+
this._fields.push({type: 'MAX', field: field});
59+
return this;
60+
},
61+
62+
/**
63+
* Executes the aggregate query and returns results as an object.
64+
* Keys are aggregate type or type_field (for field aggregates).
65+
*/
66+
execute: function() {
67+
var self = this;
68+
69+
if (this._fields.length === 0) {
70+
throw new Error("At least one aggregate function must be added.");
71+
}
72+
73+
this._fields.forEach(function(agg) {
74+
if (agg.field) {
75+
self._ga.addAggregate(agg.type, agg.field);
76+
} else {
77+
self._ga.addAggregate(agg.type);
78+
}
79+
});
80+
81+
this._ga.query();
82+
83+
var results = {};
84+
if (this._ga.next()) {
85+
this._fields.forEach(function(agg) {
86+
var key = agg.field ? agg.type + '_' + agg.field : agg.type;
87+
var value = agg.field ? self._ga.getAggregate(agg.type, agg.field) : self._ga.getAggregate(agg.type);
88+
results[key] = agg.type === 'COUNT' ? parseInt(value, 10) : parseFloat(value);
89+
});
90+
} else {
91+
// No rows matched, all aggregates 0 or null
92+
this._fields.forEach(function(agg) {
93+
var key = agg.field ? agg.type + '_' + agg.field : agg.type;
94+
results[key] = 0;
95+
});
96+
}
97+
98+
return results;
99+
},
100+
101+
type: 'SimpleGlideAggregate'
102+
};

0 commit comments

Comments
 (0)