Using OrderBy method multiple time in Script?

Vivek Verma
Mega Sage
Mega Sage

Hi Experts,

I need to understand the, How to use OrderBy method multiple times in script.

find_real_file.png

11 REPLIES 11

Namrata Khabale
Giga Guru

Hey Vivek,

I would like to add @Aniket Sawant 's point,

 

orderBy(String fieldName)

Specifies a field name to be used to order the query set. This may be called more than once to order by multiple fields.

Example:

 function UpdateProjectWBS(project) {

 var count = 0;  var child = new GlideRecord('pm_project_task'); 

child.addQuery('parent', project.sys_id); 

child.orderBy('order'); 

child.orderBy('number'); 

child.query(); 

var len = child.getRowCount().toString().length; 

var seq = 0;  while (child.next())

{    count += UpdateProjectTaskWBS(child, 1, ++seq, len, ''); 

gs.addInfoMessage(count + ' Project Tasks updated');

}

orderByDesc(String, fieldName)

Specifies a field used to order the query set in descending order.

 

For More information Refer the Link:

 

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/r_GlideRecord-orderByD...

 

Mark my Answer Correct and Helpful if it works for you.

Thanks

Namrata.

 

Hi Namrata,

Would you please share the full script,

Function "UpdateProjectTaskWBS" is not define there.

Chuck Tomasi
Tera Patron

I use this all the time to sort within a sort. For example, if I have a list of music. I can call it once to sort on artist, then call it again to sort on title within the list of artists. Very useful.

music.orderBy('artist');
music.orderBy('title');

Would produce something like:

Adam Ant - Goody Two Shoes

Adam Ant - Strip

Billy Joel - Uptown Girl

Billy Joel - You may be right

Boston - More than a feeling

 

find_real_file.pngThank you, Chuck!

Still, I am a bit confused.

var child = new GlideRecord('incident'); 
child.addEncodedQuery('active=true');
child.orderByDesc('u_testdatetimeone');
child.orderByDesc('u_testdatetimetwo');
child.query(); 
 if (child.next())
{   
  var cNumber = child.u_testdatetimeone; 
  var dNumber = child.u_testdatetimetwo; 
  gs.print(cNumber);
  gs.print(dNumber);
}

 

This Code Output is --

2020-01-30 06:17:55
2020-02-05 00:00:00

 But What exactly I am expecting here is -

2020-01-30 06:17:55
2020-02-07 06:12:16

 

Hi Vivek,

I'm confused. I don't see the "2020-02-07 06:12:16" date in either screenshot you attached or the original post. Where is that coming from?