Dynamic GlideAggregate to avoid repetition of code and getRowCount
Hi Everyone,
I am a frequent ServiceNow Community User. I am sharing my views on how to avoid getRowCount and make a reusable function. Hope you enjoy reading this code description.
/*------------Code Description---------------------*/
To get Aggregate and avoid getRowCount and repetations for simple queries Parameter Description table = table name like 'incident' encodedQuery = Encoded query needed to be passed if any like 'active=true' operation = Like (COUNT, SUM, MIN, MAX, AVG) field = any field on which you want the aggregate to run example var table = 'incident'; var encodedQuery = 'active=true'; var operation = 'COUNT'; var field = '';
var count = new SST_Util().getAggregate(table,encodedQuery,operation,field);
/*-------------------------Code-------------------*/
getAggregate: function(table, encodedQuery, operation, field) {
var answer = 0;
operation = '' + operation.toUpperCase();
var agg = new GlideAggregate('' + table);
agg.addEncodedQuery('' + encodedQuery);
if (JSUtil.nil(field)) {
agg.addAggregate('' + operation);
} else {
agg.addAggregate('' + operation, '' + field);
}
agg.orderByDesc('sys_updated_on');
agg.setGroup(false);
agg.query();
if (agg.next()) {
if (JSUtil.nil(field)) {
answer = agg.getAggregate('' + operation);
} else {
answer = agg.getAggregate('' + operation, '' + field);
}
}
return answer;
}
Do let me know if this is useful.
Ratish Kumar
Practice Lead-ServicePortal and Implementations
Labels:
https://www.servicenow.com/community/developer-articles/dynamic-glideaggregate-to-avoid-repetition-of-code-and/ta-p/2321890
