- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2017 11:33 AM
Hi,
I have to created scripted rest api ,To get all the incident number from caller_id .
Thanks in advance
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2017 02:16 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2017 12:10 PM
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2017 12:33 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2017 12:56 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2017 01:26 PM
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