GlideRecord orderBy

Puru2
Tera Contributor

Good Eve all !

I have a short query regarding GlideRecord *orderBy* clause.

What happens when certain records are set on same order ?

Let's say we queried a custom table and added a condition to sort the data according to ascending order

of a field : *u_rank*.

Code Snippet :

var rec = new GlideRecord('u_plant_coorporates);

rec.orderBy('u_rank');

rec.query();

while(rec.next())

{

   //do something

}

If there are 3 records set on same rank (let's say : u_rank=1  for all 3 records), then does it take only 1st record and skips rest of them ?

What if we want other records as well (set on the same u_rank) ?

 

Best Regards,

Puru

6 REPLIES 6

scott barnard1
Kilo Sage

If they have the same order it ranks then in alphabetical order based on the system language

regards

Chalan B L
Giga Guru

var rec = new GlideRecord('incident');
rec.addQuery('priority','1'); // just for checking purpose added
rec.orderBy('priority');
rec.query();
while(rec.next()){

gs.print(rec.number);

}

 

try to run the above script in background scripts where all the priority 1 records it has taken

Hope after running the script clarifies your doubts

 

Mark the answer as correct and helpful, if it helped you,..!!

 

Regards,

Chalan

Rajdeep Ganguly
Mega Guru

Hello Puru, The GlideRecord orderBy() function in ServiceNow does not exclude any records. It simply sorts the records based on the field specified. If multiple records have the same value for the field specified in the orderBy() function, all of those records will be included in the result set. The order in which they appear is not guaranteed and may depend on other factors such as the order in which they were inserted into the database. Here's a summary: - The orderBy() function sorts the records based on the field specified. - If multiple records have the same value for the field specified in orderBy(), all of those records will be included in the result set. - The order of records with the same orderBy() value is not guaranteed. - If you want to ensure a specific order for records with the same orderBy() value, you can add additional fields to the orderBy() function. For example, rec.orderBy('u_rank', 'u_name') would sort by u_rank first, and then by u_name within records with the same u_rank. - The orderBy() function does not exclude any records from the result set.