- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 08:19 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2023 08:06 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2023 08:06 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2023 05:45 PM
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 -
Adobe Portable Document Format (PDF) | application/pdf |
.xls | Microsoft Excel | application/vnd.ms-excel |
.xlsx | Microsoft Excel (OpenXML) | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
.doc | Microsoft Word | application/msword |
.docx | Microsoft Word (OpenXML) | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2023 06:57 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 12:33 PM
Hi Ankur
Do you have any base64encoded string for doc and xls that I can use to test? Just basic - nothing fancy.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 10:11 PM
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>
}
]
}