query on Task table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2020 11:52 PM
below query returns 50 records order by priority
var tsk= new GlideRecord('task');
tsk.chooseWindow(0, 50);
tsk.orderBy('priority');
tsk.query();
while(tsk.next()){
gs.log(tsk.priority + " - " + tsk.short_description);
}
below query should return first 3 records that comes from above query. But it is not coming like. Order is not same like first query.If i say order, that mean, priority with short_description
var tsk= new GlideRecord('task');
tsk.chooseWindow(0, 3);
tsk.orderBy('priority');
tsk.query();
while(tsk.next()){
gs.log(tsk.priority + " - " + tsk.short_description);
}
Any help in this, please.
- Labels:
-
Integrations

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2020 12:07 AM
Hi,
That is expected because in 50 records there can be 25 Priority 1 records. So, when script is executed to display 3 it will take any of random P1 from 25 records.
If you want same records with both queries you need to add more queries. Something like
tsk.addQuery('number','IN000.....');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2020 12:17 AM
Thanks for your reply.
I am pulling the task for pagination. So I have to pull records with the page limit 3.
May you please elaborate on the query that I have to add to get the same records.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2020 12:24 AM
Try using below & check for results.
var tsk= new GlideRecord('task');
tsk.chooseWindow(0, 3);
tsk.orderBy('priority');
tsk.orderBy('sys_created_on');//additonal check by created date
tsk.query();
while(tsk.next()){
gs.log(tsk.priority + " - " + tsk.short_description);
}
Output:
var tsk= new GlideRecord('task');
tsk.chooseWindow(0, 50);
tsk.orderBy('priority');
tsk.orderBy('sys_created_on');//additonal check by created date
tsk.query();
while(tsk.next()){
gs.log(tsk.priority + " - " + tsk.short_description);
}
Output:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2020 12:15 AM
Hi,
the 2nd query works for me i.e. it orders by priority
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader