ServiceNow Learning 75: "addTrend" Usage in Scripts in ServiceNow.

Shamma Negi
Kilo Sage
Kilo Sage

Hi All,

 

I have found this function which we use alot in reportings in ServiceNow. Here is the usage in the script. Please have a look:

 

 

GlideAggregate - addTrend(String fieldName, String timeInterval, Number numUnits)

Adds a trend for a field.

 

Name Type Description
fieldNameStringName of the field for which trending should occur.
timeIntervalStringTime interval for the trend.
Valid values:
  • date
  • dayofweek
  • hour
  • minute
  • quarter
  • value
  • week
  • year
numUnitsNumberOptional. Only valid when timeInterval = minute. Number of minutes to include in the trend.

Default: 1

 

 

 

Example

 

 

var trend = new GlideAggregate('incident');  
trend.addTrend ('opened_at','month');  
trend.addAggregate('COUNT');  
trend.setGroup(false);  
trend.query();  
while(trend.next()) {  
   gs.print(trend.getValue('timeref') + ': ' + trend.getAggregate('COUNT'));  
}  

 

 

Output:

9/2023: 3
10/2023: 8
11/2023: 14
 
This gives the opened incident each month trend.
Hope this helps you.
 
I hope this article helpful. Please mark it as helpful and bookmark if you like it.
 
Regards,
Shamma
 
Regards,Shamma Negi
2 REPLIES 2

Pooja58
Kilo Sage

Hi,
If I want to pass the month and year dynamically then how can I do that?
Example I need a count of Incident created in August month 2024?

Kindly reply.

Pooja

This Below solution worked for me

var year = '2022';
var month = 'aug';
var fullMonth = 'August';
var gr = new GlideAggregate('incident');
gr.addEncodedQuery("u_date_and_timeDATEPART"+fullMonth+"@javascript:gs.datePart('month','"+month+"\','EE')^u_date_and_timeDATEPART"+year+"@javascript:gs.datePart('year','"+year+"\','EE')");
gr.addAggregate('COUNT');
gr.query();
while(gr.next()){
gs.log(gr.getAggregate('COUNT'));