Using REST API with Servicenow

sanjaydhonde
Kilo Contributor

I have few questions, It would be great if someone could answer them.

  1. How to fetch the incidents sorted by time?
  2. One REST API query returns approx. 501 Incidents. How to get the next set of incidents? [required for pagination]
  3. REST API to create/update/delete/view Knowledge ? Any Documentation?

Pradeep Sharma

1 ACCEPTED SOLUTION

jose_valle
ServiceNow Employee
ServiceNow Employee

Hi Sanjay,



For pagination, you can make use of the sysparm_offset and sysparm_limit parameters.




For example the following will retrieve 5 records (sysparm_limit=5) in the record set starting from the beginning (sysparm_offset=0) of the record set.



/api/now/v1/table/incident?sysparm_limit=5&sysparm_offset=0&sysparm_query=active=true^ORDERBYnumber



The get the next 5 records, you would simply increment the value of sysparm_offset by the value of your sysparm_limit



/api/now/v1/table/incident?sysparm_limit=5&sysparm_offset=5&sysparm_query=active=true^ORDERBYnumber



Another thing that helps is that you will actually get a link header in the response that provides the urls to first,prev,next and last pages based on values used for sysparm_offset and sysparm_limit. so you might just look into that to get the url to use to get the next page.



Hope this helps!


-Jose


View solution in original post

7 REPLIES 7

Andrii
Kilo Guru

REST API - ServiceNow Wiki


REST API Explorer - ServiceNow Wiki



fetch from where to where?



Inside of ServiceNow RESTMessage you will find HTTP Methods ... "get", for example .. drill down into it preferences and click "Preview Script Usage" ... so you may use script ordering of data you receive.


tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Sanjay,



With regards to your Query 1.


You can use a encoded query copied from a list view.




http://wiki.servicenow.com/index.php?title=Table_API


..


4 Methods  

4.1 GET /api/now/v1/table/(tableName)

This method retrieves multiple records for the specified table with proper pagination information.



Supported Parameters


Parameter Description
sysparm_query

An encoded query.


For example: (sysparm_query=active=true)(sysparm_query=caller_id=javascript:gs.getUserID()^active=true)



Note: The encoded query provides support for order by. To sort responses based on certain fields, use the ORDERBY and ORDERBYDESC clauses in sysparm_query. For example, sysparm_query=active=true^ORDERBYnumber^ORDERBYDESCcategory filters all active records and orders the results in ascending order by number first, and then in descending order by category.



Best Regards



Tony


Thanks Tony


jose_valle
ServiceNow Employee
ServiceNow Employee

Hi Sanjay,



For pagination, you can make use of the sysparm_offset and sysparm_limit parameters.




For example the following will retrieve 5 records (sysparm_limit=5) in the record set starting from the beginning (sysparm_offset=0) of the record set.



/api/now/v1/table/incident?sysparm_limit=5&sysparm_offset=0&sysparm_query=active=true^ORDERBYnumber



The get the next 5 records, you would simply increment the value of sysparm_offset by the value of your sysparm_limit



/api/now/v1/table/incident?sysparm_limit=5&sysparm_offset=5&sysparm_query=active=true^ORDERBYnumber



Another thing that helps is that you will actually get a link header in the response that provides the urls to first,prev,next and last pages based on values used for sysparm_offset and sysparm_limit. so you might just look into that to get the url to use to get the next page.



Hope this helps!


-Jose