|
| 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 | + } |
0 commit comments