Getting specific records by relative date

KB15
Giga Guru

How would I script to get a count of records relative to the current date? This wouldn't be a range. It would be specific to a specified number of days like 3 days ago, 2 days ago, etc. (eg Get the number of incidents created 3 days ago relative to the current day)

I tried to see if I could get a filter going by using "Created relative on 3 days ago" but I get zero results. I can easily get a range of records by using on before or after but I can't seem to get records when specifying a specific number of days.

Ultimately, I need to get the difference between incidents created vs resolved per day over 3 days. It wouldn't be a total over 3 days.

1 ACCEPTED SOLUTION

KB15
Giga Guru

It seems like this is a bug that ServiceNow won't fix. The ability to choose a day relative to the current day is not possible per PRB1017188.

View solution in original post

14 REPLIES 14

Allen Andreas
Administrator
Administrator

Hello,

I assume you're doing this for a report? You're not really saying how you're trying to obtain this data and you've posted this in the Developer Community section of the forums...so here we'd think scripting...not reporting.

Just trying to clarify.

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

It wouldn't necessarily be for a report. The request is for a notification to go out if the number if new incidents > number of resolved incidents for 3 consecutive days.

My thought is to run a scheduled job at the end of each day and calculate this in a script. If there is an easier way, any ideas would be great.

ggg
Giga Guru

this will get you incidents created within the last 3 days:

var gr = new GlideAggregate('incident');
gr.addEncodedQuery('sys_created_onRELATIVEGE@dayofweek@ago@3');
gr.addAggregate('COUNT');
gr.query();
if (gr.next()){
    n = gr.getAggregate('COUNT');
}
gs.print(n);

 

write a similar script to get incidents resolved within last 3 days (state =  6)

compare the counts.

Thanks for that. Why doesn't the list filter work the same way?