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

@Ankur Bawiskar I have a similar requirement. I have few field values to be mapped along with an attachment. I am not able to attach the file from here. How should I pass the file here? I would be passing "image", "pdf", and "excel" files. So the data will have to create a record and attach the file to it. I am using the follwing script: 
How should I pass the data in the payload?
{
  "incidentNumber": "INC000124",
  "fileName": "my file.txt",
  "contentType": "text/plain",
  "fileData": "my sample file"  ------- Should it be base64 encoded text??
} 

I tried like this but it did not work

 

(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 shortDescription = parsedData.shortDescription
	var fileName = parsedData.fileName;
	var fileContentType = parsedData.contentType;
	var fileData = parsedData.fileData;

	var rec = new GlideRecord('incident');
	rec.initialize("short_description",shortDescription )
	var sysID = rec.insert()

      var gr = new GlideRecord("incident");
      gr.addQuery("sys_id",sysID);
      gr.query()
	if(gr.next()){
		var sa = new GlideSysAttachment();
		sa.write(gr, 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);

If you can guide me here, it would be a great help.

Thanks in advance..!


It is working fine for me when i use below sample JSON in postman application.

it is creating new file attaching . when i directly attach file with binary option.it is not working . kindy let me know how can i send existing file than creating new one?

 

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

@rajeevsnow 

what do you mean by sending existing file?

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

i mean instead of it creates new file, i would like to attach the image from binary option in postman?

Ankur this works for plain text file (txt) only. Do you have any generic code that works for jpg, docx etc files ?