Anyone know how to select the top 5 rows using a gliderecord query?

DrewW
Mega Sage
Mega Sage

Anyone know how to select the top 5 rows using a gliderecord query? Trying to avoid a loop counter.

6 REPLIES 6

Christopher_Mal
ServiceNow Employee
ServiceNow Employee

I haven't tried this yet, but would this work? I am not sure if the value is sysparm_rowcount exactly.



var gr = new GlideRecord("table");
gr.addQuery('sysparm_rowcount', '<=', '5');
gr.query();


Brad Tilton
ServiceNow Employee
ServiceNow Employee

You can add an orderby/orderbydesc query and setlimit to get a top five.



var gr = new GlideRecord('table');
gr.orderByDesc('sys_created_on');
gr.setLimit(5);
gr.query();

Would give you the 5 most recently created records on a table.


Yup, this works.

DrewW
Mega Sage
Mega Sage

That did not work, I also tried "rowcount" and "row_count".

This is in a UI Macro so it mite be I am not escaping the '<' properly.