Scripted REST API for inbound code is not working

Balakrishna_ABK
Tera Guru

I have created Scripted REST API for inbound create incident using POST method, while testing I can see only incident is creating but not test values are populating in the same incident, please help me in modifying the following code which I used.

 

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


var gr = new GlideRecord("incident");
var requestBody = request.body.nextEntry();
gs.log("requestBody: "+requestBody,"rest");
var sd = requestBody.short_description;
gs.log("sd: "+sd,"rest");
gr.initialize('short_description',request.body.short_description);
gr.initialize('description', request.body.description);
gr.initialize('priority', request.body.priority);
gr.initialize('category', request.body.category);
gr.initialize('sub_category', request.body.sub_category);
gr.initialize('correlation_id', request.body.correlation_id);
gr.initialize('type', "soc_inc");
var socincid = gr.insert();
return gr.getValue('number');
})(request, response);

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

seems you are not sending the JSON request correctly and not correctly parsing it

it should be something like this

body:

{
  "short_description":"test",
  "description":"test1"
}

Script:

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

	// implement resource here
	var incomingBody = request.body.dataString;
	var parsedData = JSON.parse(incomingBody);
	var desc = parsedData.description;
	var shortDesc = parsedData.short_description;

	var gr = new GlideRecord("incident");
	gr.initialize();
	gr.description = desc;
	gr.short_description = shortDesc;
	gr.insert();

	var body = {};
	body.number = gr.number;

	response.setBody(body);

})(request, response);

Regards
Ankur

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,

seems you are not sending the JSON request correctly and not correctly parsing it

it should be something like this

body:

{
  "short_description":"test",
  "description":"test1"
}

Script:

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

	// implement resource here
	var incomingBody = request.body.dataString;
	var parsedData = JSON.parse(incomingBody);
	var desc = parsedData.description;
	var shortDesc = parsedData.short_description;

	var gr = new GlideRecord("incident");
	gr.initialize();
	gr.description = desc;
	gr.short_description = shortDesc;
	gr.insert();

	var body = {};
	body.number = gr.number;

	response.setBody(body);

})(request, response);

Regards
Ankur

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

Thanks Ankur for your quick reply, its working.

Can you please share code for the update incident as well.

Balakrishna_ABK
Tera Guru

Can you please share code for the update incident as well.