Hello friends, welcome back to my YouTube channel, Basico ServiceNow Learning. In today's video, we are going to talk about one of the ServiceNow APIs called GlideAggregate. We will discuss all the GlideAggregate functions in depth, supported by use cases. By the end of this video, you will have a good understanding of all the GlideAggregate functions and their aggregation capabilities in detail.
GlideAggregate is an extension of GlideRecord and provides the capability to perform aggregations like min
, max
, count
, sum
, average
, and more. This API is primarily used for calculations in ServiceNow.
We will explore four use cases to understand the practical implementation of GlideAggregate:
cmdb_ci
table.cmdb_ci
table records as a comma-separated string.Before starting with GlideAggregate, let's look at how GlideRecord can be used to find the total count of incidents.
var count = new GlideRecord('incident');
count.query();
var totalCount = count.getRowCount();
gs.info(totalCount); // Outputs total count of incidents.
var count = new GlideAggregate('incident');
count.addAggregate('COUNT');
count.addQuery('active', true);
count.query();
if (count.next()) (
gs.info(count.getAggregate('COUNT'));
)
var count = new GlideAggregate('incident');
count.addAggregate('COUNT', 'category');
count.query();
while (count.next()) (
gs.info('Category: ' + count.category + ' Count: ' + count.getAggregate('COUNT', 'category'));
)
cmdb_ci
Tablevar count = new GlideAggregate('cmdb_ci');
count.addAggregate('SUM', 'cost');
count.query();
if (count.next()) (
gs.info(count.getAggregate('SUM', 'cost'));
)
var count = new GlideAggregate('cmdb_ci');
count.addAggregate('AVG', 'cost');
count.query();
if (count.next()) (
gs.info(count.getAggregate('AVG', 'cost'));
)
var count = new GlideAggregate('cmdb_ci');
count.addAggregate('MIN', 'cost');
count.query();
if (count.next()) (
gs.info(count.getAggregate('MIN', 'cost'));
)
var count = new GlideAggregate('cmdb_ci');
count.addAggregate('MAX', 'cost');
count.query();
if (count.next()) (
gs.info(count.getAggregate('MAX', 'cost'));
)
cmdb_ci
Table Recordsvar count = new GlideAggregate('cmdb_ci');
count.addAggregate('GROUP_CONCAT_DISTINCT', 'sys_class_name');
count.query();
if (count.next()) (
gs.info(count.getAggregate('GROUP_CONCAT_DISTINCT', 'sys_class_name'));
)
Q1: What is GlideAggregate in ServiceNow? A: GlideAggregate is an extension of GlideRecord in ServiceNow, which provides the capability to perform aggregation functions such as min, max, count, sum, and average.
Q2: How do I find the count of active incidents using GlideAggregate? A: You can initialize a GlideAggregate object, add the 'COUNT' aggregate, apply the filter for active incidents, and then query the table:
var count = new GlideAggregate('incident');
count.addAggregate('COUNT');
count.addQuery('active', true);
count.query();
if (count.next()) (
gs.info(count.getAggregate('COUNT'));
)
Q3: Can I use GlideAggregate to determine the number of incidents in each category?
A: Yes, you can use the addAggregate
function with COUNT
and the groupBy
function for the 'category' field.
Q4: How can I perform sum, average, min, and max aggregations in a table?
A: You can use the addAggregate
function with 'SUM', 'AVG', 'MIN', or 'MAX' as required.
Q5: How to get all distinct class values in a cmdb_ci
table?
A: You can use the addAggregate
function with 'GROUP_CONCAT_DISTINCT':
var count = new GlideAggregate('cmdb_ci');
count.addAggregate('GROUP_CONCAT_DISTINCT', 'sys_class_name');
count.query();
if (count.next()) (
gs.info(count.getAggregate('GROUP_CONCAT_DISTINCT', 'sys_class_name'));
)
I hope this article was helpful. Please like, share, and subscribe to the channel. If you have any questions or feedback, feel free to comment below. Thank you, and have a nice day!
In addition to the incredible tools mentioned above, for those looking to elevate their video creation process even further, Topview.ai stands out as a revolutionary online AI video editor.
TopView.ai provides two powerful tools to help you make ads video in one click.
Materials to Video: you can upload your raw footage or pictures, TopView.ai will edit video based on media you uploaded for you.
Link to Video: you can paste an E-Commerce product link, TopView.ai will generate a video for you.