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

Hi Maneesh,



You can write the script inside the scripted REST API similar to the below screenshot,


find_real_file.png



-Udhay


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


maneesh3
Tera Contributor

Hi Udhay,



Thanks a lot for your worthy response.



I am getting following error: is something i am doing wrong:



find_real_file.png




find_real_file.png


Forgot to post:



response error:




com.glide.rest.domain.ServiceException: Service error: attempt to access request body is not allowed when request method is GET or DELETE"


Hi Maneesh,



Since you are using GET method, try replacing line no 11 with the below code


var reqid = request.pathParams.rec_number;



-Udhay


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


maneesh3
Tera Contributor

Awesome. It worked.



Thanks Udhay for your most value help. I really appreciate your quick response.




A small query here:



This table has related list connected to other table:



In this how can i include that fields as well. Please help me in this last.




Thanks