get all incidents number from caller_id via scripted rest api

Sneha39
Mega Guru

Hi,

I have to created scripted rest api ,To get all the incident number from caller_id .

Thanks in advance

1 ACCEPTED SOLUTION

antin_s
ServiceNow Employee
ServiceNow Employee

Hi Sneha,



The below script should work.



(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {




var jsonStr = {"incidents":[]};


var gr = new GlideRecord('incident');


gr.addQuery('caller_id',request.queryParams.caller_id);


gr.query();



while(gr.next()){


var incidentOb =   {};


incidentOb.number = gr.number + '';


incidentOb.short_description = gr.short_description + '';


jsonStr['incidents'].push(incidentOb);


}



return jsonStr;




})(request, response);



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


View solution in original post

18 REPLIES 18

antin_s
ServiceNow Employee
ServiceNow Employee

Also, I would strongly suggest to use Table API for these queries, as you dont have to maintain these scripts. If it is for your learning purpose, it is fine. Otherwise, these are the requirements why Table APIs are available for.



Thanks


Antin


Hi Antin,



I tried using sys_id also in Shsishir's code through rest api explorer but its not showing   any response( not even response box) .



can you please let me how can I achieve( get all the incident number of a caller when I pass caller_id in the query parameter through rest api explorer)




var body = request.body.data;


  var caller_id = body.caller_id;


  var gr = new GlideRecord('incident');


  gr.addQuery('active',true);


  gr.addQuery('caller_id', "=", "c9884ac30a0a0bdb3879454e8f02774e");


  gr.query();


while (gr.next()) {






return {


"number": gr.number(),


}




Thanks


Hi Sneha,



I am getting response back with as below. Are you not getting the response back when you try in this way?



find_real_file.png


find_real_file.png


Hi Shishir,


Thanks so much for your reply.



But I am looking for scripted rest api not table api .


antin_s
ServiceNow Employee
ServiceNow Employee

Hi Sneha,



The below script should work.



(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {




var jsonStr = {"incidents":[]};


var gr = new GlideRecord('incident');


gr.addQuery('caller_id',request.queryParams.caller_id);


gr.query();



while(gr.next()){


var incidentOb =   {};


incidentOb.number = gr.number + '';


incidentOb.short_description = gr.short_description + '';


jsonStr['incidents'].push(incidentOb);


}



return jsonStr;




})(request, response);



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin