- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2022 03:56 AM - edited 10-03-2022 04:05 AM
Hi I want to how to write the scripted REST API to get the list numbers of request items.
Expect output:
requestItems = ["RITM12345", "RITM23456"]
query param = "title example"
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2022 04:45 AM
script will look like this
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try {
var arr = [];
var itemName = request.pathParams.itemname;
var ritm = new GlideRecord("sc_req_item");
ritm.addQuery("cat_item.name", itemName);
ritm.setLimit(2);
ritm.query();
while(ritm.next()) {
arr.push(ritm.getValue('number'));
}
var responseBody = {};
responseBody.data = arr;
responseBody.status = "Success";
response.setBody(responseBody);
}
catch (ex) {
var message = ex.message;
}
})(request, response);
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2022 04:01 AM
Hi,
can you share what the incoming request/endpoint will look like and what script did you try so far?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2022 04:06 AM
yes , thanks.
I add the code in the question description.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2022 04:45 AM
script will look like this
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try {
var arr = [];
var itemName = request.pathParams.itemname;
var ritm = new GlideRecord("sc_req_item");
ritm.addQuery("cat_item.name", itemName);
ritm.setLimit(2);
ritm.query();
while(ritm.next()) {
arr.push(ritm.getValue('number'));
}
var responseBody = {};
responseBody.data = arr;
responseBody.status = "Success";
response.setBody(responseBody);
}
catch (ex) {
var message = ex.message;
}
})(request, response);
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2022 06:03 AM
thanks, it worked.
> ritm.getValue('number')
helped me.