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

karthikdurga
Mega Expert

Here is the code. Hope this is helpful.



var gr = new GlideRecord('incident');


gr.addQuery('caller_id','876sdfjhds');//pass the sys id of caller id here


gr.query();


while(gr.next()){


var request = new sn_ws.RESTMessageV2();


request.setEndpoint('https://dev35036.service-now.com/api/now/table/incident/' + gr.sys_id + '?caller_id=' + gr.caller_id);


request.setHttpMethod('GET');



//Eg. UserName="admin", Password="admin" for this code sample.


var user = 'admin';


var password = 'admin';



request.setBasicAuth(user,password);


var response = request.execute();


gs.log(response.getBody());//You can do whatever you want with the response


}


karthikdurga
Mega Expert

Please see the screenshot below. As caller_id is a reference field, you need to pass the sys id of the user.


Capture.JPG


Hi Karthik,



I am using scripted rest api so running the api through that kindly try this through scripted rest api( please let me know if i am not doing it right ? but I want this to be done through scripted api only) . - Thanks




find_real_file.png


Hi Sneha



Can you please clarify the following so that I can understand and guide you properly.


1) Is your requirement to get all the incidents through a script ? or through API explorer ? The code that I first provided will allow you to achieve the former and the screenshot the later. You can use the code that I shared in any other service now system and get the incidents for a particular called id of a specific service now Instance.


2)I see that the table you selected in the screenshot is not incident table, Also the method used is GET Record (only single record), and not Records(multiple records).



I want to share a good video about REST API that would really simplify these concepts for you - link below


ServiceNow Integration (REST) - YouTube