Want to limit query to 50 records at a time while making a GET query.

Chandresh
Tera Guru

Hello All,I want to limit

I want to limit query to 50 records at a time while making a GET call to Service Now. I have checked there is one parameter(sysparm_limit) by which I can limit the number of queries at a time but I want that if number of records are 10000 then it should get all the records but in 50 records per query fashion. Any idea how to achieve this.

1 ACCEPTED SOLUTION

The idea of having sysparm_limit and link header is exactly to support this scenario. When you make your first call on load of page, you pass sysparm_limit =30 as query parameter.


With the response, you will get next, prev link in headers. when ever you scroll down, you make next call to get next 30 records using next and keep going


It is recommended to use ORDERBY so you don't get duplicate records because of changes in DataBase plan which might re order results causing duplicates



For eg. My initial request for 10 records looks like this


https://instance.service-now.com/api/now/table/sys_user?sysparm_limit=10&sysparm_query=ORDERBYsys_cr...



Response will have Link header


Link →


<https://instance.service-now.com/api/now/table/sys_user?sysparm_limit=10&sysparm_query=ORDERBYsys_cr...>;rel="first",


<https://instance.service-now.com/api/now/table/sys_user?sysparm_limit=10&sysparm_query=ORDERBYsys_cr...>;rel="next",


<https://instance.service-now.com/api/now/table/sys_user?sysparm_limit=10&sysparm_query=ORDERBYsys_cr...>;rel="last"



If you see here there is link for rel=next, which gives you next 10 records. There are libraries out there which can parse this header elegantly to get proper links for next, first,last set of records.


In this case, we need to get 'rel=next' link and make subsequent request to get next 10 records


https://instance.service-now.com/api/now/table/sys_user?sysparm_limit=10&sysparm_query=ORDERBYsys_cr...


Response will have another set of headers with again next, prev link to get that set of records



Link →


<https://instance.service-now.com/api/now/table/sys_user?sysparm_limit=10&sysparm_query=ORDERBYsys_cr...>;rel="first",


<https://instance.service-now.com/api/now/table/sys_user?sysparm_limit=10&sysparm_query=ORDERBYsys_cr...>;rel="prev",


<https://instance.service-now.com/api/now/table/sys_user?sysparm_limit=10&sysparm_query=ORDERBYsys_cr...>;rel="next",


<https://instance.service-now.com/api/now/table/sys_user?sysparm_limit=10&sysparm_query=ORDERBYsys_cr...>;rel="last"


View solution in original post

11 REPLIES 11

Hi Deepak,



Could you please explain below examples given in the above link, what is the signoificance of rel="next", "prev" etc.



                                                                                                                  *****************************************************************************************


Link: REST message data can be split into multiple result sets rather than forcing the user to submit multiple requests. The header has different links available for the first set, previous set, next set, and the last set of records, where applicable.


For example:
{instance}/api/now/table/cmdb_ci?sysparm_offset=40&sysparm_limit=10000>;rel="next",
{instance}/api/now/table/cmdb_ci?sysparm_offset=40&sysparm_limit=10000>;rel="prev",
{instance}/api/now/table/cmdb_ci?sysparm_offset=0&sysparm_limit=10000>;rel="first",
{instance}/api/now/table/cmdb_ci?sysparm_offset=2780&sysparm_limit=10000>;rel="last"



Note: The sysparm_limit parameter defaults to 10,000 records. This limit can be set to any value, however, an unusually large value can impact system performance.



X-Total-Count: The total count of records returned by query.



                                                                                                                  *****************************************************************************************


Hi Chandresh,



I have not tried it yet, but reading from the description what I could make out is like



1) rel="next" must be providing you the next set of data , so you might have to use only this method with proper offset and sysparm_limit value



2) rel="prev" must be providing you the previous set of data and so on



I would strongly suggest to wait for an update from someone else who have actually worked on it OR if it is urgent for you, I would suggest to use "Chrome REST APIs extension" to query against the demo servicenow instance with permutations and combinations to check what does it mean



I would use the GET method against Incident table with sysparm_limit of 20 and will set offset limit of 5


Thank you Deepak for your response, Well I have tried a different way and it is working fine. I have used chooseWindow query method. I have created a processor which I can call and passed the parameters in chooseWindow query method.



Below is the syntax:



8.12 chooseWindow(Number f, Number l, Boolean forceCount)