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

Kent13
Kilo Expert

Try using Relative instead of Between and then copy that as your encoded query

find_real_file.png

 

Example please. What do we mean by Relative?

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);
}

Awesome.... Any reference to documentation please. Thank you.