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

I was getting 1 record and were very close to use to push for all records.


Still it is your solution Shishir. I just tuned the script a bit. You owe the credit.


that's ok Antin. Learned new way, how to push data in JSON type variable.


Hello,


I need to create a Webservice API where input is: Sys ID of any record and output is: all *related* active records to that sys id. The relationships between the tables is maintained in Relationships under system definition.


Could you please guide me how to implement this? Sample code would be great.


Input: Sys ID of the record
Output: All *RELATED* active records from *VARIOUS* tables (in the following JSON format):
{
"result": [
{
"Sys ID": "5520267",
"CI Name": "Record 1",
"Table Name": "u_table_a"
},
{
"Sys ID": "5520367",
"CI Name": "Record 2",
"Table Name": "u_table_a"
},
{
"Sys ID": "8331210",
"CI Name": "Record 1",
"Table Name": "u_table_b"
},
{
"Sys ID": "8321210",
"CI Name": "Record 2",
"Table Name": "u_table_b"
},
{
"Sys ID": "3042006",
"CI Name": "Record 3",
"Table Name": "u_table_b"
},
{
"Sys ID": "4509847",
"CI Name": "Record 1",
"Table Name": ""u_table_c"
}
{
"Sys ID": "4509247",
"CI Name": "Record 2",
"Table Name": ""u_table_c"
}
]
}

Response on Postman:



find_real_file.png