- 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
Hi Sneha,
Please try below code.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var incidentnum={};
var writer = response.getStreamWriter(),
hdrs = {};
hdrs['Content-Type'] = 'application/json';
response.setStatus(200);
response.setHeaders(hdrs);
var gr = new GlideRecord('incident');
gr.addQuery('caller_id',request.pathParams.caller_id);
gr.query();
writer.writeString("{\"results\":[");
while(gr.next())
{
incidentnum.inc_number = gr.number + '';
writer.writeString(global.JSON.stringify(incidentnum));
gs.log(incidentnum.inc_number);
}
writer.writeString("]}");
})(request, response);
Result:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2017 12:25 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2017 12:30 PM
Please pass the sys_id in caller_id
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2017 12:34 PM
Hi Sneha,
As per Shishir's code, it is looking Caller's sys_id as the input for the variable 'caller_id' and not the User's name. If you pass sys_id, it will work.
If your requirement is to pass User's name only, you need to change the script to refer the 'name' field as follows.
gr.addQuery('caller_id.name',request.pathParams.caller_id);
Hope this helps. Mark the answer as correct/helpful based on impact.
Thanks
Antin