Quick and Dirty Glide Aggregate Flow Designer action
I just wanted to post a quick and dirty GlideAggregate Flow Designer Action I threw together.
The goal:
To run a GlideAggregate query against a table to retrieve all the unique values that are found for a given field value. Say I have a bunch of records, and I want to find out how many unique assignees I might have, so that I can send a bunch of notifications to them, I would feed this action the name of the table, an Encoded Query, and the field I'm interested in seeing the values for.
The Execution:
Start by creating a new action. I've called mine "FlowAggregate".
For inputs, I want a Table Name (type table name), a Field to retrieve (Field List or String), and a non-mandatory "Encoded Query" string:
Once I have the inputs, then I want to pass them through a Glide Aggregate query, and produce a stringified array:
(function execute(inputs, outputs) {
var tableName = inputs.table_name;
var fieldName = inputs.field_name;
var encQuery = inputs.encoded_query;
var resultArr = [];
var count = new GlideAggregate(tableName);
if (encQuery != '') {
count.addEncodedQuery(encQuery);
}
count.addAggregate('COUNT',fieldName);
count.query();
while(count.next()){
var field = count[fieldName];
var fieldCount = count.getAggregate('COUNT',fieldName);
gs.log("there are currently "+ fieldCount +" records with a "+fieldName+" of "+ field);
resultArr.push(field.getValue());
}
outputs.value_array = resultArr.toString();
})(inputs, outputs);
(NOTE: the "getValue()" against the resultsArr object may have some problems with application scopes. You might be able to replace with "toString()" instead.)
I'll also need to make sure I declare that output of "value_array":
Finally, I need that action output:
With all this in place, I'm able to query a table, then get all the unique values for that query as a handy-dandy comma-separated string, against which I can perform additional Flow operations.
https://www.servicenow.com/community/now-platform-articles/quick-and-dirty-glide-aggregate-flow-designer-action/ta-p/2295392