Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Posting Attachments through Scripted REST API

anthonydalferro
Tera Guru

We have a requirement where a third part application needs to send an attachment to Service Now and attach it to a record.  We cannot use the out of box Attachment API as that requires the Sys Id of the record being updated and they won't have it.  So I am building a Scripted Rest API based on the following GlideSysAttachment command.

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/no-namespace/c_GlideSysA...

 

I have tried to add this sample code from the documentation and updated the sys id of an incident that exists in my instance.

var attachment = new GlideSysAttachment();

var rec = new GlideRecord('incident');

var incidentSysID = 'ab1b30031b04ec101363ff37dc4bcbfc';

rec.get(incidentSysID);

var fileName = 'example.txt';

var contentType = 'text/csv';

var base64Encodedcontent = 'SSBhbSB0ZXh0Lg==';

var agr = attachment.writeBase64(rec, fileName, contentType, base64Encodedcontent);

gs.info('The attachment sys_id is: ' + agr);

 

When I run this through fix script to test the logic, I get the following message,

GlideTime - unparseable date: invalid function
*** Script: The attachment sys_id is: undefined

And the attachment does not get added.  What am I doing wrong?  Should this work through a fix script as a test?

5 REPLIES 5

Sumanth16
Kilo Patron

Hi @anthonydalferro ,

 

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);

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

 

Thanks & Regards,

Sumanth Meda

I need to use base64 as that is the data I will be receiving.  

Gangadhar Ravi
Giga Sage

@anthonydalferro  Please check below and see if helpful.

 

https://www.servicenow.com/community/developer-forum/sending-attachment-using-rest-api/m-p/1786194

 

Please mark my answer correct and helpful if this works for you.

That is for sending attachments through a business rule out of ServiceNow.  I need to accept an attachment.