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

In case of related lists, you have to query those tables individually to fetch desired field values and use the same in your response body.



-Udhay


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


maneesh3
Tera Contributor

Thanks Udhay.



If I want to call that table in a different REST request, then is there any possibility to call the list of records in a table which is depend on some record number, there are multiple entries:



as example: record number: '123' and it has 50 different entries in the table



Can you please guide me with code if that has to be called by list of items. Final help




Thanks


Maneesh, i couldn't understand your last comment.



-Udhay


maneesh3
Tera Contributor

HI Udhay,



Let us say we have an table name: "Sample Issues" and   table has 5 fields



Record number: Abc123 and it has 50 records with same record number (Abc123 )



In this case, we have to show all 50 records with those 5 fields information if in the request 3rd party sends : Abc123 as request.





Thanks,


please try the below code,



********************************************************


var reqid = request.pathParams.rec_number;


var body ={};


var content = '';


var gr = new GlideRecord('table_name');


gr.addQuery('number',reqId);


gr.query();



while(gr.next){


content = content+'\n'+' Number:'+gr.number+' caller_id:'+gr.caller_id+' priority:'+gr.priority+' short_description:'+gr.short_description+' state:'+gr.state;


}


if(content == '' ){


body.status = 'error';


body.content = 'No record found';


}


else{


body.status = 'success';


body.content = content;


}



response.setContentType();


response.setStatus(200);


response.setBody(body);



return response;



****************************************************



-Udhay


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