Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Getting count in REST API

Arjun King
Tera Contributor

Hi ,

How to get row count  for table API in Rest.In simple i want to get  no of records extracted using Table api.

Ex:

https://instance.service-now.com/api/now/table/incident?sysparm_query=sys_created_onBETWEENjavascript:gs.dateGenerate('2019-04-03','00:00:00')@javascript:gs.dateGenerate('2019-04-03','16:59:59')^incident_state=4&sysparm_display_value=true&sysparm_exclude_reference_link=true&sysparm_fields=number%2Ccaller_id%2Cincident_state%2Ccontact_type%2Csubcategory%2Cu_tertiary_category%2Cshort_description%2Cu_vendor_name%2Cu_alternate_contact_number%2Cu_alternate_phone%2Cu_alternate_email_address%2Ccategory%2Cimpact%2Curgency%2Cpriority%2Copened_by%2Copened_at%2Cu_case_type%2Cu_next_action_2%2Cu_next_action_1%2Cassigned_to%2Cu_callback_start_date%2Cclose_code%2Cu_closure_codes_2%2Cclose_notes%2Cdescription&sysparm_limit=100&sysparm_offset=0    --> for this i need to get the count of extracted records.

 

Thanks,

Arjun.

9 REPLIES 9

Harsh Vardhan
Giga Patron

why dont you use Aggregate API - GET /now/stats/{tableName}

 

further details you can refer the below link. 

 

https://docs.servicenow.com/integrate/inbound_rest/reference/r_AggregateAPI-GET.html

 

 

Hi Harshavardhan,

using aggregate API, how can we fetch the values of number. Since aggregate API is used for the operation b/w the fields. but i want to get the fields along with the Count using Table API.

 

Thanks,

Arjun.

you can try with below script:

 

var request = new sn_ws.RESTMessageV2();
var headers = request .getRequestHeaders();
request.setEndpoint('https://dev34768.service-now.com/api/now/table/incident?sysparm_query=active%3Dtrue&sysparm_limit=1');
request.setHttpMethod('GET');

//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'admin';
var password = 'admin';

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");

var response = request.execute();
gs.print(response.getBody());

gs.print(response.getHeader("X-Total-Count"));

Morten Johansen
Giga Contributor

Can't you use the count from the response? (X-Total-Count: Total count of records returned by the query.) - Link

In PowerShell I would to this to get the count as an example:

 

# Eg. User name="admin", Password="admin" for this code sample.
$user = "admin"
$pass = "admin"

# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))

# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')

# Specify endpoint uri
$uri = "https://devXXXXX.service-now.com/api/now/table/incident?sysparm_query=active%3Dactive&sysparm_limit=1"

# Specify HTTP method
$method = "get"

# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri 

# Print response
$response.RawContent

# Print count of returned records
($response.headers)."X-Total-Count"

hamid laghari1
Tera Contributor

use this var totalRecords = response.getHeader("X-Total-Count");
you will get total number of records