How to order GlideRecord query output vy datetime field

Milan13
Giga Expert

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); }

 

1 ACCEPTED SOLUTION

Oh Great that things got working & you got to know the issue. Kindly close the thread by marking appropriate answer as correct.

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

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);

}

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideRecordSco...

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Jaspal Singh
Mega Patron
Mega Patron

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); }

Date/Time thanks.

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()");