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,



Since you were able to achieve your requirement and you are not able to view the button for marking it correct, could you endorse the content so that it has a visibility. Thanks in advance.



Regards


Ankur


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

Hi @Ankur Bawiskar ,

My usecase is to parse the email(.msg) file attached to a RITM# record(sc_req_item table). I've used the following script:

var gr = new GlideRecord('sys_attachment');
var sa = new GlideSysAttachment();
var stringUtil = new GlideStringUtil();

gr.addEncodedQuery('table_name=sc_req_item^table_sys_id='+current.sys_id);
gr.query();
while(gr.next())
{
var binData = sa.getBytes(gr);
var encData = stringUtil.base64Encoe(binData);
var data_string = stringUtil.base64Decode(encData);
gs.print(data_string);
}

The output I'm receiving as - 

�w^~)�

 

Can you please help me?

Hi @Tippireddy you have mistaken the spelling for Encode. see it should be-

Var encData = stringUtil.base64Encode(binData);

@Manish65 After I did that my output is nothing absolutely, FYI my attachment file is a txt file

Hi @Ankur Bawiskar I am using this script but its not working 

 error:

Javascript compiler exception: The choice of Java constructor java.lang.String matching JavaScript argument types (null) is ambiguous; candidate constructors are: 
    String(byte[])
    String(java.lang.StringBuilder)
    String(java.lang.StringBuffer)
    String(char[])
    String(java.lang.String) (null.null.script; line 19) in: