- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2020 03:09 AM
Hello,
I have a problem to sort output of a GlideRecord query by datetime field (Date/Time field).
Even if I convert the date/time field to number value, the orderBy function does not work, any idea why?
Appreciate your help,
Milan
var rec = new GlideRecord('sc_request');
rec.orderBy("u_date_from.getGlideObject().getNumericValue()");
rec.query();
while(rec.next()) {
gs.print(rec.number + ' - ' + rec.u_date_from); }
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2020 03:39 AM
Oh Great that things got working & you got to know the issue. Kindly close the thread by marking appropriate answer as correct.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2020 03:17 AM
Hi,
you can use orderByDesc() or orderBy();
try this
var rec = new GlideRecord('sc_request');
rec.orderBy("u_date_from");
rec.query();
while(rec.next()) {
gs.print(rec.number + ' - ' + rec.u_date_from);
}
If my answer solved your issue, please mark my answer as ✅ Correct & 👍Helpful based on the Impact.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2020 03:18 AM
What type is u_date_from field? Date or Date-Time.
Also, try below.
var rec = new GlideRecord('sc_request');
//rec.orderByDESC("u_date_from.getGlideObject().getNumericValue()");
rec.orderBy('u_date_from');
rec.setLimit(3); //set limit to 3 & check if you get only 3 values printed for a check
rec.query();
while(rec.next()) {
gs.print(rec.number + ' - ' + rec.u_date_from); }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2020 03:24 AM
Date/Time thanks.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2020 03:26 AM
Thanks Milan.
Then using rec.orderBy('u_date_time'); should be enough to do the trick in case of discrepancy if any instead of
rec.orderBy("u_date_from.getGlideObject().getNumericValue()");