GetBase64 content of attachment

fabrizio95360
Tera Expert

I'm trying to send an attachment in base64, when I print in the logs the base64 string format doesn't appear, I don't understand why, this is my code:

 

 

 

var grAttach = new GlideRecord('sys_attachment');
grAttach .get('01ecf2f84785395002ccefb2e36d438f');
var attachment = new GlideSysAttachment();
gs.info('ATTACHMENT '+attachment.getContentBase64(grAttach));

 

 

 

 

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @fabrizio95360 ,

 

Try the following code and this works in Global scope too.

 

var StringUtil = new GlideStringUtil();
var gsis = GlideSysAttachmentInputStream("01ecf2f84785395002ccefb2e36d438f");
var baos = new Packages.java.io.ByteArrayOutputStream();
gsis.writeTo(baos);
baos.close();
var base64Data = StringUtil.base64Encode(baos.toByteArray());
gs.info('ATTACHMENT ' + base64Data);

 
Please mark my answer helpful and accept as solution if it helped you 👍✔️

 

Thanks,
Anvesh

View solution in original post

3 REPLIES 3

AnveshKumar M
Tera Sage
Tera Sage

Hi @fabrizio95360 ,

 

Try the following code and this works in Global scope too.

 

var StringUtil = new GlideStringUtil();
var gsis = GlideSysAttachmentInputStream("01ecf2f84785395002ccefb2e36d438f");
var baos = new Packages.java.io.ByteArrayOutputStream();
gsis.writeTo(baos);
baos.close();
var base64Data = StringUtil.base64Encode(baos.toByteArray());
gs.info('ATTACHMENT ' + base64Data);

 
Please mark my answer helpful and accept as solution if it helped you 👍✔️

 

Thanks,
Anvesh

Hi @fabrizio95360 

 

Please mark my answer helpful and accept as solution if it helped you, otherwise please let me know if you need any more help.

 

 

Thanks,
Anvesh

Hi Anvesh,

 

The script is helpful, but I am still seeing the scoped error in the logs when executing it and do you know if how we can send this base64 data as key pair value in the json body, would be helpful.

 

I am sending it after the above code usage this way :

 

 
var body=
{
  'sys_id': sysidstring,
'attachment' : base64Data,
        'attachment_name' : attachmentname
 
};
 
var bodyString=JSON.stringify(body);
 request1.setRequestBody(bodyString);

 

 

Thank you,

Laxmi.