How to send attachments to 3rd party system through Base64?

User205031
Tera Contributor

Hi,

We are integrating with 3rd party system flow designer for sc_req_item table, we need to pass the attachment to them in base64 format.  In the script action in the flow ,used the below code but returning null to me.

 

var gr = new GlideRecord('sys_attachment');
  gr.addQuery('table_sys_id', inputs.bulk_csv);
  gr.query();
  while(gr.next()){
    var sysAtt = new GlideSysAttachment();
    var base64Data = sysAtt.getContentBase64(gr);
    gs.info(base64Data);
  }
outputs.attach = base64Data;
 
User205031_0-1739258148572.png

The 'attach' variable is returning null.

 

How to fix this?

Thanks in advance

 

7 REPLIES 7

Viraj Hudlikar
Tera Sage

Hello @User205031 

Check this thread:
Solved: Re: Converting attachment to base64 in Flow Design... - ServiceNow Community

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Ranjit Nimbalka
Mega Sage

Hi @User205031 

 

You can try below code:-

            table= sys_attachment;

            record=record;

            var gsa = new GlideSysAttachment();
            var bytesInFile = gsa.getBytes(table, record);
            var base64string = GlideStringUtil.base64Encode(bytesInFile);
            return base64string;
 
 
Regards,
Ranjit

Ankur Bawiskar
Tera Patron
Tera Patron

@User205031 

flow is in which scope?

the getContentBase64() method is only for scoped app, so if your flow is for scoped app then it should work fine provided the sys_attachment record is found and the output variable is mapped correctly

For global scope use this

var attGr = new GlideRecord('sys_attachment');
  attGr.addQuery('table_sys_id', inputs.bulk_csv);
  attGr.query();
  if(attGr.next())
        {
   var gsu = (typeof GlideStringUtil != 'undefined') ? (GlideStringUtil) : (Packages.com.glide.util.StringUtil); //few versions support the first one, other supports second
   var gsa = (typeof GlideSysAttachment != 'undefined') ? (new GlideSysAttachment()) : (new Packages.com.glide.ui.SysAttachment());
   var attachmentData = gsa.getBytes(attGr);
   var attachment = String(Packages.java.lang.String(attachmentData));
   var encData = GlideStringUtil.base64Encode(attachmentData); // data of one attachment at a time
outputs.attach = encData;
}

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

@User205031 

Hope you are doing good.

Did my reply answer your question?

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