Last active
January 9, 2023 17:51
-
-
Save johncaruso/980715f7b6a87e92f5ad3ff089f44848 to your computer and use it in GitHub Desktop.
Get the aggregate SUM of a field using ServiceNow GlideAggregate API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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