How to fetch specific columns in response using scripted rest api?

Tara16
Giga Contributor

Hi All,

I am able to produce all fields from table using scripted rest api. What is the code or syntax to fetch some specific columns only?

Thanks,

Tara

4 REPLIES 4

Voona Rohila
Kilo Patron
Kilo Patron

Hi tara

Can you share the code you tried to get all fields.


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Prashant16
Kilo Guru

Hi Tara, 

If you are using get method you have an option in REST API explorer as shown below:

 

find_real_file.png

 

 

Response:

find_real_file.png

Code will look like this:

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://dev64130.service-now.com/api/now/table/sn_hr_core_case?sysparm_fields=state%2Csys_created_by&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.log(response.getBody());

 

 

Thanks,

Prashant

 

Please mark my answer Correct & helpful if its gives your solution.

Code will look like this:

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://dev64130.service-now.com/api/now/table/sn_hr_core_case?sysparm_fields=state%2Csys_created_by&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.log(response.getBody());

Hi Tara,

Please mark my answer correct and helpful if it resolved your issue.

 

Thanks,

Prashant