Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Use 'greater than or equal to' to compare two date fields

JJG
Kilo Guru

Hello,

I have a script include that searches a table. It should pull records if the 'pay_period_end' date field is greater than or equal to today's date. It doesn't seem to work, is there an error? It works when I remove my grPayPeriod.addQuery line.

get_payPeriod: function() {

var dateToday = new GlideDate();
var grPayPeriod = new GlideRecord('x_utsll_time_manag_hour_tracker');
grPayPeriod.addQuery('pay_period_end' >= dateToday);
grPayPeriod.query();

if (grPayPeriod.next()) {
return grPayPeriod.getValue('pay_period');
}
},

1 ACCEPTED SOLUTION

sachin_namjoshi
Kilo Patron
Kilo Patron

Use below code

 

get_payPeriod: function() {

var dateToday = new GlideDate();
var grPayPeriod = new GlideRecord('x_utsll_time_manag_hour_tracker');
grPayPeriod.addQuery('pay_period_end', ">=",dateToday);
grPayPeriod.query();

if (grPayPeriod.next()) {
return grPayPeriod.getValue('pay_period');
}

 

Also, check useful blog for date field validations.

 

https://community.servicenow.com/community?id=community_blog&sys_id=467c62e1dbd0dbc01dcaf3231f9619ad

 

Regards,

Sachin

View solution in original post

6 REPLIES 6

sachin_namjoshi
Kilo Patron
Kilo Patron

Use below code

 

get_payPeriod: function() {

var dateToday = new GlideDate();
var grPayPeriod = new GlideRecord('x_utsll_time_manag_hour_tracker');
grPayPeriod.addQuery('pay_period_end', ">=",dateToday);
grPayPeriod.query();

if (grPayPeriod.next()) {
return grPayPeriod.getValue('pay_period');
}

 

Also, check useful blog for date field validations.

 

https://community.servicenow.com/community?id=community_blog&sys_id=467c62e1dbd0dbc01dcaf3231f9619ad

 

Regards,

Sachin

This wont give proper results because of time difference.

GlideDate() will give you date in your timezone whereas the fields value in the backend is in GMT.

 

-Tanaji

Please mark reply correct/helpful if applicable

You can't assume that since you don't know poster's instance.

I was correcting typo in his code.

Anyway, blog has solution to validate date fields.

 

Regards,

Sachin

I understand you corrected typo and I really appreciate helping others.

Having said that you can't assume poster is a male;)

 

-Tanaji

Please mark reply correct/helpful if applicable