Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get the list(array) request item numbers according to the item name in sc_req_item

Bowen Lu
Tera Contributor

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.

jsキャプチャ.PNG

 

1 ACCEPTED SOLUTION

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);
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

can you share what the incoming request/endpoint will look like and what script did you try so far?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

yes , thanks.

I add the code in the question description.

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);
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

thanks, it worked.

 > ritm.getValue('number')

 helped me.