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

Ankur Bawiskar
Tera Patron
Tera Patron

@Saquib Mohammed 

what's your exact requirement here? are you getting base64encoded data from 3rd party and want to attach file to record?

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

Hi Ankur
Yes, that is correct. I am getting base64encoded data, file name and content type for an incident task identified by the correlation id. I need to attach the file to that incident task record. 

I had put some log lines and identified that the write() function fails. Also, the decodedBytes() does not decode the base64Encodedcontent as expected. For my test, I used base64 encoded string as VGhpcyBpcyB0ZXh0 which should decode to This is text, but it decoded to [B@13ac87a

@Saquib Mohammed 

this script will work in global scope provided the base64encoded data is correct

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

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

I ran this script. Though it does throw any error, no attachment was added to the incident task record.

I added some logs that I have attached here. Base64 string seems to be correct because I am able to decode and get the expected text when I use it on https://base64.guru/converter/decode/text