ServiceNow Learning 75: "addTrend" Usage in Scripts in ServiceNow.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2023 06:30 AM
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
fieldName | String | Name of the field for which trending should occur. |
timeInterval | String | Time interval for the trend. Valid values:
|
numUnits | Number | Optional. 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
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
- 665 Views
2 REPLIES 2
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 08:55 AM
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
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 10:33 AM
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'));