Find nth position in a GlideRecord query

gatesgopal
Kilo Explorer

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?

4 REPLIES 4

mamann
Mega Guru

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


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.


millsdude
Kilo Contributor

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.



//Find the last 10 incidents created
var gr = new GlideRecord('incident');
gr.orderByDesc('sys_created_on');
gr.chooseWindow(10, 20);
gr.query();


harikrish_v
Mega Guru

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