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

my reading of your requirement was that you wanted the incidents within your parameters relative to the last 3 days.

if that is not the case please restate your requirement clearly

 

It's all records for a given day and not a range of days relative to the current date. It was in the original question I believe.

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.

singhdev
Tera Contributor

You could use 'relative' filter twice to narrow down the number of days ago, first one with 'on or after' and second with 'before'. 

For e.g.  

you can copy the encoded query if you want to use it in a script.

This worked for me.

Carl-Johan
Tera Expert

Sometimes I like to reply to these "old" questions when I have a similar problem and finds a workaround.

In this case I would do this (using this in a scheduled job) to get records which have a exact due date 5 days from today.

It seems like somewhat the same thing that you where having trouble with.

Part of my script:

var upcomingDueDate = new GlideDateTime();

//add 5 days to current date

upcomingDueDate.addDaysLocalTime(5);

//build querystring to use in gliderecord call later on - query only finds tasks which have this exact due date

var queryString ="state=-6^due_dateON" + upcomingDueDate.getDate() + "@javascript:gs.dateGenerate('" + upcomingDueDate.getDate() + "','start')@javascript:gs.dateGenerate('" + upcomingDueDate.getDate() + "','end')";

....of course you then use the querystring variable in a regular gliderecord call.

Maybe it can be done in a better way - but it works for me 😉