Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

query on Task table

MO26
Kilo Explorer

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.

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

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.....'); 

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.

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:

find_real_file.png

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:

find_real_file.png

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

the 2nd query works for me i.e. it orders by priority

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader