Byte #2: GlideAggregate Examples

Note: This was originally posted on my personal blog back on 2014. However, I’ve reviewed it for accuracy and found that it’s still relevant and London compatible!

 

Previously, I posted an example of how to get distinct records using GlideAggregate. Today I want to share some further powerful examples of GlideAggregate.

First off, you can grab the JavaScript file for GlideAggregate showing all the functions you can use, from right here: GlideAggregate JS. Alternatively, you can always grab the entire library of Service-Now JavaScript objects and functions.

Key Functions

  • addAggregate(type) – Add a new statistical function to the query, such as Count, AVG, Max.
  • groupBy(field) – The field you’re running statistics on. If you want to know how many items are assigned to someone, you’ll group by the field “assigned_to”
  • orderByAggregate(field) – If you’re looking for who is assigned the most items, you would order by “count”.
  • getAggregate(type) – Use this inside your while loop to get the Count, AVG, Max statistic that you were querying for.
  • addHaving(type, operator, value) – This is a great way to be able to limit your results. For example, I can write a query to only return users that have at least 10 active tasks assigned to them.
    Note: addHaving is now only available in the GLOBAL scope.

Example Background Scripts

Activate All Roled Users

Count Incidents Opened This Month

Count Incidents Resolved Today

Count Incidents Resolved Yesterday

Find Tasks with more than one SLA of the same type

Note: This will now only work in the GLOBAL scope.

Helpful Function to get Average Survey Scores

Have More Examples?

If you have any creative uses of GlideAggregate, please feel free to share them in the comments! And as always, never hesitate to ask any questions you might have.