Send attachment as binary

Jose MMR
Tera Guru

Hello everyone.

I'm working on a scripted rest.
My idea is to take all the attachments of a record, transform them into binary and send them through the rest.

I've been trying but none of the methods I've seen have worked for me. For example:

 

var attachments = new GlideRecord('sys_attachment');
            attachments.addQuery('table_sys_id',sysID of my record.toString());
            attachments.query();
            while (attachments.next())
            {              
               
                var r = new sn_ws.RESTMessageV2();
                r.setRequestBodyFromAttachment(singleattachmentsysid.sys_id.toString()); //seems this action convert attachtment to binary
                var body = r.getRequestBody();
                gs.log(body + " for attachmentid: " + attachments.sys_id);
   
            }
 
 
Answers for this script:
*** Script: null for attachmentid: c7XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Is there a way to get all the attachments in binary in an array?
All the best.
1 ACCEPTED SOLUTION

Jose MMR
Tera Guru

I found this post:

 

How to send attachment files in base64 format?

 

I solved finally with:

 

                var sa = new GlideSysAttachment();
                var StringUtil = new GlideStringUtil();
                //Get the base64 encoding of the attachment
                var binData = sa.getBytes(singleattachmentsysid);
                var encData = GlideStringUtil.base64Encode(binData);
 
 

View solution in original post

3 REPLIES 3

Aylee Andersen
Kilo Sage

Hi @Jose MMR,

 

Do you have  to have it in binary, or would base64 be acceptable? I can help you convert to base64 via ServiceNow methods, but you'd probably have to use some JavaScript wizardry outside of that to get it to binary.

 

Let me know if that would be helpful and I'll type something up!

- Aylee

Hi Aylee,

 

I think that base 64 is a good way. Could you guide me to obtain any attachment as pdf, jpg...to base64?

 

Regards.

Jose MMR
Tera Guru

I found this post:

 

How to send attachment files in base64 format?

 

I solved finally with:

 

                var sa = new GlideSysAttachment();
                var StringUtil = new GlideStringUtil();
                //Get the base64 encoding of the attachment
                var binData = sa.getBytes(singleattachmentsysid);
                var encData = GlideStringUtil.base64Encode(binData);