Using OrderBy method multiple time in Script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2020 04:45 AM
Hi Experts,
I need to understand the, How to use OrderBy method multiple times in script.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2020 05:19 AM
Hey Vivek,
I would like to add
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:
Mark my Answer Correct and Helpful if it works for you.
Thanks
Namrata.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2020 06:00 AM
Hi Namrata,
Would you please share the full script,
Function "UpdateProjectTaskWBS" is not define there.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2020 06:13 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2020 08:45 AM
Thank 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 |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2024 01:26 PM
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?