Find nth position in a GlideRecord query
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-24-2013 12:33 AM
Hi All,
Can someone please help me out to find out a way to find the nth position of a glide record when a table is queried?
My use case is that I need to query a table from a client script, find its nth position, say 1st record and 5th record and update these values to another table using the same client script?
Can this be done using client script or is there a better way I can use?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-26-2013 09:47 AM
You can use the GlideRecord method getRowNumber() to help with this.
You can check the Row Number to see if it's where you want it to be, if not, in your loop, use continue; to skip to the next record.
http://wiki.servicenow.com/index.php?title=GlideRecord#getRowNumber
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-26-2013 10:49 PM
Hi Mark, Thanks for the reply.
I was looking for something like this but not the getRowNumber() exactly. I am aware of that getRowNumber() could be used to find nth position.
Actually I want this to work in a business rule, but then I was trying it in a client script simultaneously.
But my use case is such that getRowNumber() shouldn't be used. Anyway I appreciate your help. Thanks a lot.
Thanks for the comment.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2014 11:45 PM
I know this is an old post, but I found the answer from a servicenowguru gliderecord cheatsheet. Thought it would be good to share.
The chooseWindow(first,last) method lets you set the first and last row number that you want to retrieve and is typical for chunking-type operations. The rows for any given query result are numbered 0..(n-1), where there are n rows. The first parameter is the row number of the first result you'll get. The second parameter is the number of the row after the last row to be returned. In the example below, the parameters (10, 20) will cause 10 rows to be returned: rows 10..19, inclusive.
var gr = new GlideRecord('incident');
gr.orderByDesc('sys_created_on');
gr.chooseWindow(10, 20);
gr.query();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2014 11:14 AM
Hi Rajaa,
You might want to use a business rule for this rather than a client script, then again it depends on your requirement. Whenever you need to do a query, server side scripting is advised as it is more efficient.
For you to fetch the nth record, you might want to sort the records in that table and then check for a particular index. You can use the below query:-
var gr=new GlideRecord('<table_name');
gr.orderByDesc('sys_created_on'); //sorting by descending order of created on date
gr.chooseWindow(15,16);//this will fetch you the 15th record. If you keep it as 5,10-you will gets records 5,6,7,8,9
gr.query();
if(gr.next()){
var gr1=new GlideRecord('<second table name>'); //querying the second table to copy values
gr1.addQuery('<field_name>','value'); //adding query to retrieve the right record
gr1.query();
if(gr1.next()){
gr1.<field_1>=gr.<field_1>; //copying fields
gr1.<field_2>=gr.<field_2>;
gr1.update(); //updating the record
}
}
I also suggest that you go through this post in Servicenowguru, it will be really helpful.
» GlideRecord Query Cheat Sheet
Hope this helps
Thanks & Regards,
Hari