Skip to content

Instantly share code, notes, and snippets.

@johncaruso
Last active January 9, 2023 17:51
Show Gist options
  • Select an option

  • Save johncaruso/980715f7b6a87e92f5ad3ff089f44848 to your computer and use it in GitHub Desktop.

Select an option

Save johncaruso/980715f7b6a87e92f5ad3ff089f44848 to your computer and use it in GitHub Desktop.
Get the aggregate SUM of a field using ServiceNow GlideAggregate API.
/**
* Get the aggregate SUM of a field using ServiceNow GlideAggregate API.
*
* @param {string} table Name of table to query
* @param {string} field Name of numeric field to SUM
* @param {string} [query] Optional encoded query string
* @returns {number} Aggregate SUM of field
*/
function getFieldSum(table, field, query) {
var ga = new GlideAggregate(table);
if (query) ga.addQuery(query);
ga.setGroup(false);
ga.addAggregate('SUM', field);
ga.query();
ga.next();
return Number(ga.getAggregate('SUM', field));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment