how i get the count of incidents which are created last month not in last 30 days with out specifying any date using background script

suprakash
Giga Expert

how i get the count of incidents which are created last month not in last 30 days with out specifying any date   using background script, without using any report types I dont want want to specify any date any month. suppose i am checking using background script in december   the count will display the no. of incidents for the month of november .... how can i do it??

1 ACCEPTED SOLUTION

drjohnchun
Tera Guru

Please try



var ga = new GlideAggregate('incident');
ga.addQuery('opened_atONLast month@javascript:gs.beginningOfLastMonth()@javascript:gs.endOfLastMonth()');
ga.addAggregate('COUNT');
ga.query();
var incidents_opened = ga.next() ? ga.getAggregate('COUNT') : 0;
gs.info(incidents_opened);



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

View solution in original post

12 REPLIES 12

drjohnchun
Tera Guru

Please try



var ga = new GlideAggregate('incident');
ga.addQuery('opened_atONLast month@javascript:gs.beginningOfLastMonth()@javascript:gs.endOfLastMonth()');
ga.addAggregate('COUNT');
ga.query();
var incidents_opened = ga.next() ? ga.getAggregate('COUNT') : 0;
gs.info(incidents_opened);



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

Hi john ,


Thanks for replying .. Its working, but the problem   is   i just wanted to check what time its catching using



gs.print(gs.beginningOfLastMonth());


gs.print(gs.endOfLastMonth());



in result I saw its giving the time in GMT , Can we bring the time in IST ??



Thank You


Both gs.beginningOfLastMonth() and gs.endOfLastMonth() should handle time zones automatically, so you won't have to worry about it. When you run



gs.print(gs.beginningOfLastMonth());


gs.print(gs.endOfLastMonth());



in the Background Script, the displayed time zone you see is from the user's context. Are you running it as a user who's in GMT?


Yes, you are correct   Both gs.beginningOfLastMonth() and gs.endOfLastMonth() are handling time zones automatically. In background script it is adjusting the time accordingly and showing the time in gmt ..


code--


gs.print(gs.beginningOfLastMonth());


gs.print(gs.endOfLastMonth());



output--


*** Script: 2016-10-31 18:30:00


*** Script: 2016-11-30 18:29:59



As the user is from India and the time zone will be IST


without showing 2016-10-31 12:00:00 it is automatically calculating the difference between GMT and IST and displaying the value in GMT that is 2016-10-31 18:30:00



One more thing John , is there any way by which I can display only the name of previous month , no date or time required. suppose I am writing a script in background script I need to display only the name of previous month . Like   gs.print(gs.beginningOfLastMonth());   displays the date and time   of the beginning of last month, Is there any way or method present by   which I can print only the name of previous month , like now it is December if I write the code the output will display just only November......