Query due date for todays date

Kerry10
Giga Expert

I am trying to query incidents that have a due date on today. Can someone tell me why the below code is not working, yet the comparison at the bottom returns true?

 

var gr1 = new GlideRecord('incident');
gr1.addQuery('due_date', 'STARTSWITH', gs.now());
while(gr1.next())
gs.print('queried incidents: ' + gr1.number) //returns nothing, but should return inc0010004 per below

var gr2 = new GlideRecord('incident');
gr2.query('number', 'INC0010004');
gr2.next();
gs.print(gr2.due_date.startsWith(gs.now())); //returns true

 

output:

*** Script: true
1 ACCEPTED SOLUTION

sorry, left out the gr1.query(); line before while (gr1.next()).

 

But in any case, I would replace the STARTSWITH query with what I put above.

View solution in original post

6 REPLIES 6

Jon Barnes
Kilo Sage

I would change it like this. Startswith is a string query, and not a reliable way to query dates.

 

var gr1 = new GlideRecord('incident');
gr1.addEncodedQuery('due_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
while(gr1.next())

 

btw, this is the exact query I got when I queried from the incident list for all incidents where "Due date ON Today". I then right-clicked that filter and clicked Copy Query to get the query you see above.

Still nothing 😕

 

var gr1 = new GlideRecord('incident');
gr1.addEncodedQuery('due_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
while(gr1.next())
gs.print('queried incidents: ' + gr1.number)

var gr2 = new GlideRecord('incident');
gr2.query('number', 'INC0010004');
gr2.next();
gs.print(gr2.due_date.startsWith(gs.now()));

 

gives me:

*** Script: true

sorry, left out the gr1.query(); line before while (gr1.next()).

 

But in any case, I would replace the STARTSWITH query with what I put above.

Well that makes sense!Thanks!