Scripted Rest API to update incident with attachment

Prasant Kumar 1
Kilo Sage

Hi Experts,

Can anyone help me with the code of scripted rest API to add an attachment with request payload?

 

@Ankur Bawiskar @asifnoor 

Thanks & Regards

Prasant Kumar Sahu

1 ACCEPTED SOLUTION

Hi,

Sample JSON request:

{
  "incidentNumber": "INC000124",
  "fileName": "my file.txt",
  "contentType": "text/plain",
  "fileData": "my sample file"
}

Scripted REST API Script:

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

	var requestBody = request.body.dataString;
	var parser = new global.JSON();
	var parsedData = parser.decode(requestBody);
	var number = parsedData.incidentNumber;
	var fileName = parsedData.fileName;
	var fileContentType = parsedData.contentType;
	var fileData = parsedData.fileData;

	var rec = new GlideRecord('incident');
	rec.addQuery('number',number);
	rec.query();
	if(rec.next()){
		var sa = new GlideSysAttachment();
		sa.write(rec, fileName, fileContentType, fileData);
		var responseBody = {};
		responseBody.incNumber = number;
		responseBody.status = "Success";
		response.setBody(responseBody);
	}
	else{
		var responseBodyFailure = {};
		responseBodyFailure.status = "Failure";
		response.setBody(responseBodyFailure);
	}

})(request, response);

Regards
Ankur

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

View solution in original post

25 REPLIES 25

@Prasant Kumar Sahu 

Thanks for marking my response as helpful.

If my response helped you please mark it correct to benefit future readers as well.

Regards
Ankur

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

@Prasant Kumar Sahu 

Thanks for marking my response as helpful.

If my response helped you please mark it correct to benefit future readers as well.

Regards
Ankur

 

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

@Prasant Kumar Sahu 

Thanks for marking my response as helpful.

If my response helped you please mark it correct to benefit future readers as well.

Regards
Ankur

 

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

@Prasant Kumar Sahu 

Thanks for marking my response as helpful.

If my response helped you please mark it correct to benefit future readers as well.

Regards
Ankur

 

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

@Prasant Kumar Sahu 

Thanks for marking my response as helpful.

If my response helped you please mark it correct to benefit future readers as well.

Regards
Ankur

 

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