write() function of GlideSysAttachment() not working

Saquib Mohammed
Mega Guru

I have written below script as a Scripted REST resouce in Global scope. However, the write function doesnt work

var queryParamsVar = request.queryParams;
var attachment = new GlideSysAttachment();
var correlationId = queryParamsVar.CorrelationId[0];
var grIncTask = new GlideRecord('incident_task');
grIncTask.addQuery('correlation_id', correlationId);
grIncTask.query();

var fileName = queryParamsVar.FileName[0];
var contentType = queryParamsVar.ContentType[0];
var base64Encodedcontent = queryParamsVar.Base64Enc[0];
var decodedBytes = GlideStringUtil.base64DecodeAsBytes(base64Encodedcontent);
var arg = attachment.write(grIncTask, fileName, contentType, decodedBytes);
gs.info('This is a log for attachment sys_id is: ' + agr);

 

In the system logs, I see the below error - 

*** Script: undefined: no thrown error

and

getEventTarget() called with invalid record reference: sys_poll. for event: appsec.security.export, could have been deleted

I was trying to use writeBase64 but later found that it can used only for scoped applications. So I tried with write function but this is not doing the job either.

1 ACCEPTED SOLUTION

@Saquib Mohammed 

small update

var queryParamsVar = request.queryParams;
var attachment = new GlideSysAttachment();
var correlationId = queryParamsVar.CorrelationId[0];
var grIncTask = new GlideRecord('incident_task');
grIncTask.addQuery('correlation_id', correlationId);
grIncTask.query();
grIncTask.next(); // added now

var fileName = queryParamsVar.FileName[0];
var contentType = queryParamsVar.ContentType[0];
var base64Encodedcontent = queryParamsVar.Base64Enc[0];

var eccGr = new GlideRecord('ecc_queue');
eccGr.initialize();
eccGr.setValue('agent', 'AttachmentCreator');
eccGr.setValue('topic', 'AttachmentCreator');
eccGr.setValue('name', fileName + ':' + contentType);
eccGr.setValue('source', grIncTask.getTableName() + ':' + grIncTask.getUniqueValue());
eccGr.setValue('payload', base64Encodedcontent);
eccGr.insert();

it should work for all file content and types

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

15 REPLIES 15

@Saquib Mohammed 

small update

var queryParamsVar = request.queryParams;
var attachment = new GlideSysAttachment();
var correlationId = queryParamsVar.CorrelationId[0];
var grIncTask = new GlideRecord('incident_task');
grIncTask.addQuery('correlation_id', correlationId);
grIncTask.query();
grIncTask.next(); // added now

var fileName = queryParamsVar.FileName[0];
var contentType = queryParamsVar.ContentType[0];
var base64Encodedcontent = queryParamsVar.Base64Enc[0];

var eccGr = new GlideRecord('ecc_queue');
eccGr.initialize();
eccGr.setValue('agent', 'AttachmentCreator');
eccGr.setValue('topic', 'AttachmentCreator');
eccGr.setValue('name', fileName + ':' + contentType);
eccGr.setValue('source', grIncTask.getTableName() + ':' + grIncTask.getUniqueValue());
eccGr.setValue('payload', base64Encodedcontent);
eccGr.insert();

it should work for all file content and types

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur
Thank you for the script. It works for text, csv and image files. However, when I am trying to upload doc(x), xls(s) or pdf, I am getting 505 HTTP Version Not Supported error.

What might be the issue? I am using wesite https://products.aspose.app/pdf/conversion/ to generate the base64 encoded text. I feel that the base64 or the MIME content type is causing the error. Any pointers? Is there any other way to generate the base64 version of the file that I can try

Following are the content type i am using - 

.pdfAdobe Portable Document Format (PDF)application/pdf
.xlsMicrosoft Excelapplication/vnd.ms-excel
.xlsxMicrosoft Excel (OpenXML)application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.docMicrosoft Wordapplication/msword
.docxMicrosoft Word (OpenXML)

application/vnd.openxmlformats-officedocument.wordprocessingml.document

@Saquib Mohammed 

that seems the issue. the base64encoded data is not correct hence it cannot be decoded by ServiceNow

ServiceNow supports UTF-8 encoding only

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur

Do you have any base64encoded string for doc and xls that I can use to test? Just basic - nothing fancy.

One more question - and I hope this would be the last one. The script that you provided, is that good for multiple attachments as well? If the incoming json has a correlation id and a list of attachment in the following format, can you let me know what change would be required in your script?

{

correlationId: <correlationId>,

attachment: [

  {ContentType:<contentType>,

   FileName: <filename>,

   Base64Enc: <Base64Enc>

  },

  {ContentType:<contentType>,

   FileName: <filename>,

   Base64Enc: <Base64Enc>

  }

]

}