How to get filter based on time

lucky6
Tera Contributor

Hi Everyone, 

How to get filter based on time? ex. Need cases created between yesterday 8pm to today 8pm.

Can anyone help me on this.

Thanks in advance..

6 REPLIES 6

Rahul Kumar17
Tera Guru

Hi @lucky6,

 

You can use the sys_created_on field to filter cases based on their creation time. Here's an example of how to filter cases created between yesterday 8 PM and today 8 PM:

 

 

var start = new GlideDateTime();
start.setDayBefore(1);
start.setHourOfDay(20);
start.setMinute(0);
start.setSecond(0);

var end = new GlideDateTime();
end.setHourOfDay(20);
end.setMinute(0);
end.setSecond(0);

var gr = new GlideRecord('incident');
gr.addQuery('sys_created_on', '>=', start);
gr.addQuery('sys_created_on', '<=', end);
gr.query();

while (gr.next()) {
  // Do something with the filtered incidents
}

 

 

This script creates two GlideDateTime objects, start and end, and sets them to yesterday 8 PM and today 8 PM respectively. Then it creates a GlideRecord object for the incident table, adds the sys_created_on queries to filter the records, and runs the query. Finally, it loops through the filtered records using gr.next() and performs some action on each record.

 

Thanks,

Rahul Kumar

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

Thanks for the reply..

when I added this script in rest api I am getting 500 error with cannot find function setDayBefore function in object.

Any idea on this..