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

@Pankaj2015 

for other file you can use SOAP Attachment creator and you require the following details

file name, content type, base64encoded data, record sysId, table name

 

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

Ankur can you share a code for soap attachment creator. Please share a sample code.

@Pankaj2015 

can you post a new question and tag me there as this is quite older thread?

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

HI

@Ankur Bawiskar 

Same script I used for incident table and through post man I tried but for Doc and Excl. 
I used the 

{   
"incident Number": "INC4969810",   
"filename": "test_6.doc",   
"content Type": "application/octet-stream",   
"file Data":  ""

}

 

and for Excl

{   
"incident Number": "INC4969810",   
"filename": "test_8 Equipment Delivery.xlsx",   
"content Type": "application/xlsx",   
"file Data":""

}

 

And the file is attaching to the attachment table but when I try to open from the INC4969810, I am getting this one

vinuthv_0-1677760676744.png

 

Hi Ankur,

 

I used this approach ,to add attachment to RITM record.I am doing exactly the same.

this is my json on postman:

{
  "reqNumber": "RITM2009512",
  "fileName": "my file.txt",
  "contentType": "image/png",
  "fileData": "my sample file"
}
 
and i have written the scripted rest api also,but my question is from where will postman fetch the content of the file.
Using the above approach will it create empty attachments.
 
Any help is appreciated.