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

Tanaji Patil
Tera Guru

Easiest way is to build query on the list view of the table and add copied query in addEncodedQuery.
ServiceNow will take care of rest for you.

Try this code-

get_payPeriod: function() {

var grPayPeriod = new GlideRecord('x_utsll_time_manag_hour_tracker');
grPayPeriod.addQuery('pay_period_end>=javascript:gs.beginningOfToday()');
grPayPeriod.query();

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

 

-Tanaji

Please mark reply correct/helpful if applicable.

 

Mark Roethof
Tera Patron
Tera Patron

Hi JJG,

Can you perform the same query from a list? If so, you could use the breadcrumb too your advantage. Copying that breadcrumb, and using it in your script. Read about this in an article I wrote a while back:
Utilizing the breadcrumb on lists to generate your query

Looking at your script further. You do mention querying for records. So multiple records? While you performing an if, so actually only one record? Now you will only get one records. It also does not have an order or something, so which records actually?
So is this correct?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn