Scripted Rest API Not working

rambabu1
Giga Expert

Hi All,

below is the script that i written on scripted rest api. but its not working when i send data like https://google.com/gtre

can any one help me on this

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

var obj = {};
obj.number = request.pathParams.number;
obj.worknotes = request.pathParams.work_notes;
var gr = new GlideRecord("sc_req_item");
gr.addQuery("number", obj.number);
gr.query();
if (gr.next()) {

gr.work_notes = obj.worknotes;
gr.update();
response.setBody(obj);

}

})(request, response);

 

 

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you are not hitting the ServiceNow endpoint so how the scripted rest api would run?

Regards
Ankur

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

Hi Ankur,

i created endpoint as well. here is the endpoint

PUT https://instance.service-now.com/api/396051/request/{number}/{work_notes}

but i receiving the response like.

{
  "error": {
    "detail": null,
    "message": "Requested URI does not represent any resource"
  },
  "status": "failure"
}

Hi,

why not use POST method and include body

Like this

{"number":"RITM001", "work_notes":"my testing"}

HTTP - POST

Resource path - /api/65080/testing_incident_update

Script

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

	// implement resource here	
	var incomingBody = request.body.dataString;
	var parsedData = JSON.parse(incomingBody);
	var ritm = parsedData.number;
	var group = parsedData.work_notes;
	var body = {};
	var gr = new GlideRecord("sc_req_item");
	gr.addQuery("number", ritm);
	gr.query();
	if (gr.next()) {
		gr.setValue('work_notes', group);
		gr.update();
		body.status = "Success";
	}
	else{
		body.status = "Failure";
		body.statusMessage = "RITM not found";
	}
	response.setBody(body);

})(request, response);

Regards
Ankur

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

Ankur, 

still receiving error

find_real_file.png