Issue while converting attachments to Base64 in flow designer action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi All,
We have an integration requirement where we will be sending attachments from Incidents to third party application. I am currently doing it using flow designer action.
The main isue that I am facing is the mix of base64 content when multiple attachments are attached to an Incident.
If I am adding a single attachment, it's working absolutely fine. The third party is also able to retrieve and read the attachment. But, the problem is when there are multiple files added , the base64 content gets mixed between the files. Hence, the other integrated environment are not able to read these corrupted files though they are able to retrieve it.
Here's my code -
(function execute(inputs, outputs) {
// ... code ...
var inc = new GlideRecord('incident');
inc.addQuery('sys_id',inputs.incident_sysid);
inc.query();
if(inc.next())
{
outputs.incnumber = inc.number;
outputs.postnumber = inc.correlation_id;
}
var sa = new GlideSysAttachment();
var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', inputs.attachment_sysid);
gr.addQuery('table_name', 'incident');
gr.setLimit(1);
gr.query();
var attachments = [];
if (gr.next()) {
var bytesInFile = sa.getBytes('incident', inputs.incident_sysid);
var base64string = GlideStringUtil.base64Encode(bytesInFile);
// var base64Content = sa.getBase64Content(gr);
gs.log('base64' +base64string);
attachments.push({
filename : gr.getValue('file_name'),
content_type: gr.getValue('content_type'),
content: base64string
});
}
outputs.base64 = JSON.stringify(attachments);
})(inputs, outputs);The file name, content_type are working fine but the content of base64 is giving me the issues.
Any help will be appreciated.
Thanks in Advance !
Nayan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi @nayanmule ,
I tried the below script it works for me , hope this script works for you
(function execute(inputs, outputs) {
// ... code ...
var inc = new GlideRecord('incident');
inc.addQuery('sys_id', inputs.incident_sysid);
inc.query();
if (inc.next()) {
outputs.incnumber = inc.number;
outputs.postnumber = inc.correlation_id;
}
var sa = new GlideSysAttachment();
var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', inputs.attachment_sysid);
gr.addQuery('table_name', 'incident');
gr.setLimit(1);
gr.query();
var attachments = [];
if (gr.next()) {
var bytesInFile = sa.getBytes(gr);
var base64Data = '';
if (typeof GlideStringUtil !== 'undefined') {
base64Data = GlideStringUtil.base64Encode(bytesInFile); // Encode bytes to Base64 string
} else {
// Fallback for older instances or different contexts
var StringUtil = Packages.com.glide.util.StringUtil;
base64Data = StringUtil.base64Encode(bytesInFile);
}
// var base64Content = sa.getBase64Content(gr);
gs.log('base64' + base64Data);
attachments.push({
filename: gr.getValue('file_name'),
content_type: gr.getValue('content_type'),
content: base64Data
});
}
gs.inof( JSON.stringify(attachments));
})(inputs, outputs);If my response helped, please mark it as helpful ✅
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
26m ago
@DineshK24367716 , appreciate your response . Unfortunately, your code fix didn't work in my case.
Thanks for your inputs.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
19m ago
Ok @nayanmule, could you please share screenshots showing where exactly the error occurs?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
things to correct
-> your GlideRecord is querying only 1 sysId and hence picking only 1 attachment record
-> Your current code only uses IF instead of WHILE and hence picking only 1 attachment, use WHILE so that it picks all files added to that INC sysId
-> also in getBytes() you should pass the GlideRecord object of attachment record
update as this
Note: this will form array of json objects and will give that as output and should work fine provided 3rd party is able to handle array of json objects
(function execute(inputs, outputs) {
// ... code ...
var inc = new GlideRecord('incident');
inc.addQuery('sys_id', inputs.incident_sysid);
inc.query();
if (inc.next()) {
outputs.incnumber = inc.number;
outputs.postnumber = inc.correlation_id;
}
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', inc.sys_id);
gr.query();
var attachments = [];
while (gr.next()) {
var sa = new GlideSysAttachment();
var bytesInFile = sa.getBytes(gr);
var base64string = GlideStringUtil.base64Encode(bytesInFile);
attachments.push({
filename: gr.getValue('file_name'),
content_type: gr.getValue('content_type'),
content: base64string
});
}
outputs.base64 = JSON.stringify(attachments);
})(inputs, outputs);
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader