using addQuery to compare a date field with gs.nowDateTime

patricklatella
Mega Sage

Hi all,

I'd like to use multiple addQuery statements in a GlideRecord to pick out records that meet both queries and then to have an event triggered by each record.   However my line with the date query below is not working...seems to just ignore that line.   Am I missing something for getting both addQuery statements to apply?   Thanks!

var now = gs.nowDateTime();

var sd = new GlideRecord('change_request');

sd.addQuery('u_state','Pending Implementation');

sd.addQuery('sd.end_date', '<=','now');//this line does not seem to be applying to the query

sd.query();

while(sd.next()) {

gs.eventQueue('change.reminder.notify', sd, sd.number);

1 ACCEPTED SOLUTION

Arindam Ghosh
Mega Guru

Use below script:



var query = 'u_state=Pending Implementation^end_date<javascript:gs.nowDateTime()'; // verify the original value of   Pending Implementation state and replace that


var sd = new GlideRecord('change_request');


sd.addEncodedQuery(query);


sd.query();



while(sd.next()) {


gs.eventQueue('change.reminder.notify', sd, sd.number);


}



Thanks,


Arindam


View solution in original post

4 REPLIES 4

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

remove the '' from now.   addQuery('sd.end_date','<=', now); otherwise when doing this kind of stuff with time etc. I normally build the query in the listview, then copy the query and add it using addEncodedQuery() to get it right 😃


Arindam Ghosh
Mega Guru

Use below script:



var query = 'u_state=Pending Implementation^end_date<javascript:gs.nowDateTime()'; // verify the original value of   Pending Implementation state and replace that


var sd = new GlideRecord('change_request');


sd.addEncodedQuery(query);


sd.query();



while(sd.next()) {


gs.eventQueue('change.reminder.notify', sd, sd.number);


}



Thanks,


Arindam


patricklatella
Mega Sage

thanks Goran and Arindam!



I used Arundam's script and it worked great.


Dinnu
Giga Contributor

Hey , i have also same issue but the data time is in a variable end and my addQuery does not work . Can anyone suggest ?

 

var gr1 = new GlideRecord('metric_instance');
gr1.addQuery('id',sysid);
gr1.addQuery('definition','35f2b283c0a808ae000b7132cd0a4f55');
gr1.addQuery('sys_created_on', '<=', end1); // this line does not work
gr1.query();