- 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-19-2023 08:43 PM
what's your exact requirement here? are you getting base64encoded data from 3rd party and want to attach file to record?
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-19-2023 11:51 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2023 01:07 AM
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.
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-20-2023 07:22 AM
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