how to fetch multiple record using get scripted rest api

ramancoder
Tera Contributor

ramancoder_0-1669020448880.png

not getting correct output

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @ramancoder ,

 

If you want to fetch data for multiple records and send a response, then you need to use an array of objects.  Here's how the code will look like:

 

 

var output = [];
var gr = new GlideRecord('incident');
//add your encoded query here
gr.query();
while(gr.next()){
    var temp = {};
    temp[gr.number] = gr.description.toString();
    output.push(temp);
}
​
response.setBody(output);

 

Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions. 🙂

View solution in original post

5 REPLIES 5

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @ramancoder,

 

Please use the rest api explorer for creating an API for the above requirement.

I have created an API for you as below as per your requirement please put you instance name and check.

https://<instance_name>.service-now.com/api/now/table/incident?sysparm_query=category%3Dsoftware%5Epriority%3D2&sysparm_fields=number%2Cdescription&sysparm_limit=1



Please Mark my answer as correct.

BR,
Nayan.

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

Richard Hine
Tera Guru
Tera Guru

Ramancoder,

 

Your code does not really make sense. You are retrieving details from an incident and are then placing the constructed body into the response object. If you are planning to make an API call for these details, you need to place the contstructed body into the request body and not the response body.

 

Can you give the use case of what you are trying to achieve and we will be able to help you more.

 

Thanks and Regards,

 

Richard

Community Alums
Not applicable

Hi @ramancoder ,

 

If you want to fetch data for multiple records and send a response, then you need to use an array of objects.  Here's how the code will look like:

 

 

var output = [];
var gr = new GlideRecord('incident');
//add your encoded query here
gr.query();
while(gr.next()){
    var temp = {};
    temp[gr.number] = gr.description.toString();
    output.push(temp);
}
​
response.setBody(output);

 

Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions. 🙂

Thanks sir.Executed successfully.