Ordering GlideRecord with null values

Michiel Meijler
Tera Contributor

Hi,

I need to order a list of tasks on a date field in Ascending order.

By using the orderBy('date_field') the the tasks without a value for the date_field, will be ordered on top. I want to have these tasks at the end of my resultset.

Anyone for a solution?

Michiel Meijler

ServiceNow Developing Consultant
Odysseus Group

1 ACCEPTED SOLUTION

You need to split these into 2 gliderecords. One with null and one without.


View solution in original post

12 REPLIES 12

I was afraid so too.


You know of a way to combine those two gliderecords into one?



Michiel


Not that I am aware of at this moment. Let me try to dig a bit. Will try to update the thread back if I find something.


Well you can,


This could be a script include..



var arr=[];


var str= "Your query";


var tbl=new GlideRecord('table_name');


tbl.addEncodedQuery(str+'^field_name!=NULL');


tbl.orderBy('field name');


tbl.query();


while(tbl.next()){


arr.push(tbl.getValue('sys_id')); // to store the sys id of all the records which are not having the date field as empty


}


var nl=new GlideRecord('table_name');


nl.addEncodedQuery('field_name=NULL');


nl.query();


while(nl.next()){


arr.push(nl.getValue('sys_id')); // to store the sys id of all the records which are having the date field as empty


}



return arr;


Anurag Tripathi
Mega Patron
Mega Patron

Just thinking out loud, did you try to achieve that in the list view using filters...and try to use the query generated by system via EncodedQuery??


-Anurag

Hmm. Blank will come last if you sort Z-A?