Issue while converting attachments to Base64 in flow designer action

nayanmule
Tera Guru

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

10 REPLIES 10

@Ankur Bawiskar  , if I am using while loop ,it's considering all the attachments on the Incident form.

However, adding a while loop didn't work even with the file name. I am getting the same file name as an output that was added at the very first on the incident form.

 

 

@nayanmule 

are you sure you have more than 1 file on that record?

add log and see what's the count

what's returned by subflow? share that json string here

if your subflow output is array of json objects and each object is 1 file data then your logic is correct, 3rd party is unable to handle multiple files

(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 = [];
    gs.info('row count is' + gr.getRowCount());
    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);

 

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

@Ankur Bawiskar  , yes the row count is showing as 5 , that means the number of attachments on the incident record.

 

@nayanmule 

then it will run 5 times and there will be 5 objects in that json array

you didn't share your subflow screenshots what came in output?

this output variable base64 in your script step should be of type string

share that variable details as well

try this once

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.toString()

});

}

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

@Ankur Bawiskar  , my subflow output is basically a string that contains the filename, filecontent and file extension. 

The filename and file extension is working fine but somewhere the filecontent is getting mixed with other attachment's content leading to the corruption of files.

 

nayanmule_0-1767015453485.png