GlideRecord orderBy

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2018 05:09 AM
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
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2018 05:11 AM
If they have the same order it ranks then in alphabetical order based on the system language
regards

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2018 05:12 AM
Hi
Have you gone through this docs link :-
https://developer.servicenow.com/app.do#!/api_doc?v=kingston&id=r_ScopedGlideRecordOrderBy_String

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2018 05:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2024 01:21 AM
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.