How to get the content of an Attachment?

peterraeves
Mega Guru

I uploaded a simple text file to an Incident and want to retrieve the content through javascript to pass it over an external rest call.

I'm using the following code as explained by the docs, but they do not seem to be working.

var att = new GlideRecord('sys_attachment');

att.get('file_name', 'Test.txt');

gs.print(att.getDisplayValue() + '***' + att.size_bytes + '***' + att.content_type);

var sa = new GlideSysAttachment();

var content64 = sa.getContentBase64(att);

gs.print(content64);

var content = sa.getContent(att);

gs.print(content);

My output is:

*** Script: Test.txt***11***text/plain

*** Script: undefined

*** Script: undefined

Additionally, the documentation stated that only 5MB is returned through the getContent functions. What if the document is larger than that?

16 REPLIES 16

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Peter,



Here is the script which prints the attachment content.



var   gr = new GlideRecord('sys_attachment');


gr.addQuery('sys_id', 'e9f5dcbb4f103200fc11fa218110c77c'); // sys_id of attachment record


gr.query();


if(gr.next()){


var gsa = new GlideSysAttachment();


var bytesInFile = gsa.getBytes('u_sample_table', gr.table_sys_id);


var dataAsString = Packages.java.lang.String(bytesInFile);


dataAsString = String(dataAsString);


gs.print(dataAsString);


}



If you want to transfer attachment content to external web service then mostly base64Encoded stream of attachment is sent.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Question I have after reading your post:



- Why do you convert to a string twice?


- How do I convert to base64?


- Why is getContent/getContentBase64 not working?


Hi Peter,



Answer to your questions


1) No need of toString() twice and can be removed


2) Code to print base64 data is below: just add this code below gs.print(dataAsString);



var StringUtil = new GlideStringUtil();


var sa = new   GlideSysAttachment();


var binData =   sa.getBytes(gr);


var encData =   StringUtil.base64Encode(binData);


gs.print("Encoded Data is:"+encData);



3) Check below links will help you


SN Pro Tips — Understanding Attachments in ServiceNow


Easy Base64 Encoding in ServiceNow | John Andersen



Hope this helps you.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

So if I got everything write, the following should be sufficient to send over the rest service?



var sa = new GlideSysAttachment();


var bytes = sa.getBytes(attachment.getValue('table_name'),


                                              attachment.getValue('table_sys_id'));


var content64 = GlideStringUtil.base64Encode(bytes);