Response using REST API

maneesh3
Tera Contributor

Hi,

I need help in sending response from Servicenow to Third party using REST API.

I have a custom table and need to embed Record number in   this as a request and SNOW has to send response with 10 fields information. 3rd party will not send sys_id and so i need to have an scripted REST so that Record number in request.

Please help me in this. They need only response.

Thanks

1 ACCEPTED SOLUTION

Hi Maneesh,



Please try the below code it should work.



//var reqId = request.queryParams.rec_number;


var reqId = request.pathParams.rec_number;


var respBody = {};


var req = {};


var content = [];


var i=0;



var gr = new GlideRecord('table_name');


gr.addQuery('number',reqId);


gr.query();



while(gr.next()){


req = {};


req.number = gr.number;


req.short_description = gr.short_description;


req.priority = gr.priority;


content[i] = req;


i++;


}



if(content.length == 0 ){


respBody.status = 'Error' ;


respBody.content = 'No record found';


}else{


respBody.status = 'success';


respBody.content = content;


}



response.setContentType('application/json');


response.setStatus(200);


response.setBody(respBody);



-Udhay


Please Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

20 REPLIES 20

Kamal17
Kilo Sage

Hi Maneesh,



You should create a scripted REST webservice API in your servicenow instance. It will provide you an endpoint URL, you can share this endpoint URL (including username & password) with the third party application and ask them to send the request.



You have to write the processing logic in your scripted REST webservice to handle the incoming request and provide a valid response.



You may refer the below URL for more details,


Create a Scripted REST API



-Udhay


Please Hit like, Helpful or Correct depending on the impact of the response


Surendra Raika1
Kilo Guru

response.getStreamWriter().writeString("Sys Id is :" + obj.getValue('sys_id'));



you can use this response.getStreamWriter to handle the response


Kamal17
Kilo Sage

In addtion to what Surendra mentioned, you can also use setBody() function to set the response body since you are trying to send information of 10 fields in the response body.



Eg:


var body = {};


body.field1 = 'value1';


body.field2 = 'value2';


body.field3 = 'value3';


response.setBody(body);



-Udhay


Please Hit like, Helpful or Correct depending on the impact of the response


maneesh3
Tera Contributor

Thanks Udhay and Surendra for the resonse.



I have started creating the REST API   now I am struck with response setup in code. Can you please also help me in the coding:



3rd party will send the record number



In response SNOW has to send fields value dynamically related to that record




Please help me in code logic.




Thanks