Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

DrewW
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

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

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

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.