Wednesday, September 6, 2017

ServiceNow GlideRecord Query for Past Due Records

Suppose you want to query a table with GlideRecord and get only records that are past due. In other words, you want records that have a due_date value that is before the current time. There are several community and blog posts that incorrectly suggest using gs.nowDateTime() in this sort of operation. This is incorrect because gs.nowDateTime() returns the time formatted in local time zone whereas GlideRecord comparisons expect dates to be in UTC. The correct method to use is gs.nowNoTZ().

// Get all Tasks with a due_date in the past
var gr = new GlideRecord('task');
gr.addQuery('due_date', '<', gs.nowNoTZ());
gr.query();
...

According to the ServiceNow wiki, the ServiceNow database stores dates "in the internal format, yyyy-MM-dd HH:mm:ss, and the system time zone, UTC by default." This means when performing a comparison, the both values should be in UTC.

Suppose there are the following Tasks

Number   Due date
TASK001  9/6/2017 1:10:00pm CDT
TASK002  9/6/2017 1:50:00pm CDT

Further suppose that the above script is executed at 1:48:23pm CDT. Here are the comparisons that will be run in the database, using the correct gs.nowNoTZ() function.

TASK001 will be returned, since its comparison evaluates TRUE
2017-09-06 18:10:00 < 2017-09-06 18:48:23

TASK002 will NOT be returned, since its comparison evaluates FALSE
2017-09-06 18:50:00 < 2017-09-06 18:48:23

Using the incorrect gs.nowDateTime() function, the right-hand value evaluated would be 2017-09-06 13:48:23, and neither task would be returned. In effect, the person assigned to these tasks would be given additional hours to complete their work: the difference between the local timezone and UTC.

1 comment:


  1. Iam very impressive your site gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you..

    servicenow online training hyderabad

    ReplyDelete