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

Hi Peter,



Yes that should suffice. But it all depends on how the third party accepts attachment. As I said earlier usually when we use SOAP and send/receive attachment data it is in base64 format.


Just check with the requirement.



Regards


Ankur


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

Hi Peter,



Could you also mark the answer as correct and hit like? This helps in closing the thread for the question. Thanks in advance.



Regards


Ankur


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

I know, but the button has gone missing.


Hi Peter,



As you mentioned in your earlier comment that the button has gone missing. Following is the link which can help you to mark a discussion as correct/answered.


Please mark the answer as correct and hit like. Thanks in advance.


How to mark a discussion as answered in SNOW community?



Regards


Ankur


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

The button is not there because this is not a question, but a discussion.