Incident created within past rolling 60 minutes (1 hour)

TT3
Kilo Guru

I need to pull incidents created within past 60 minutes and it should be rolling. e.g. if current date and time is 6/16/2022 14:30:00 then it should bring all the incidents created between 6/16/2022 13:30:00 and 6/16/2022 14:30:00.

I am going to use GlideDateTime to get dates but not sure how to add them in encoded query:

var gdFrom = new GlideDateTime();
var gdTo = new GlideDateTime();
gdTo.subtract(3600000);

var inc = new GlideRecord('incident');
inc.addEncodedQuery('opened_atBETWEEN'); // This is where the question is.
inc.query();
if (inc.next()) {
 gs.print(inc.number);
}
1 ACCEPTED SOLUTION

find_real_file.png

 

Once you create the query, as Allen said, right click on the very last character in the query and copy the query, then paste it in your encoded query line.  There is no need to do any calculations and no need for the GlideDateTime.

Running this in background should give you all incidents created in the last 60 minutes and display the current date/time and the date/time when the incident was created.

var timestamp = new Date().toISOString().
  replace(/T/, ' ').      // replace T with a space
  replace(/\..+/, '')     // delete the dot and everything after

var inc = new GlideRecord('incident');

inc.addEncodedQuery('sys_created_onRELATIVEGT@minute@ago@60');

inc.query();

while (inc.next()){
  gs.print('Current date/time is ' + timestamp +'. Incident # ' + inc.number + ' was created ' + inc.sys_created_on);
}

View solution in original post

9 REPLIES 9

Allen Andreas
Administrator
Administrator

Hi,

You should be able to just build this in list view like so (example):

find_real_file.png

Then right-click the last piece of breadcrumb after you run it and choose copy query, then paste that in your addEncodedQuery line.

find_real_file.png

Please mark reply as Helpful/Correct, if applicable. Thanks!


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

I tried it, but it doesn't count minutes, so let's say 2:06 PM then it will be >= 1 PM.

Hi,

Alright. Well, I see you found a solution that worked for you.

Overarching point here was to build it yourself in list view (you can figure that part out?) and then copy the query and use that in your encoded query instead of scripting it all out, etc.

Hope some of it was Helpful.

Take care! 🙂


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

Yes, and I marked your answer helpful. However, Kent's answer was more helpful as he provided screenshot etc. Thank you.

Hi,

Thanks for doing that 🙂

It wasn't marked helpful previously, which is why I wanted to double-check and see if it was. I too provided screenshots, just...not with arrows or reiterating your script and doing that part for you, haha.

Anyways, in the end, I'm just glad you got the support you needed to resolve your issue. The power in using the list view to filter accordingly and then copying that query is the core foundation when using addEncodedQuery.

Take care!


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