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

Also note that in the original code you capture the output into variable

arg

but log variable

agr

Not sure if that is just a paste error, or the code is indeed faulty.