How to get the content of an Attachment?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2017 06:03 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2017 10:48 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2020 08:34 AM
Hi
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-06-2021 03:00 AM
Hi
Var encData = stringUtil.base64Encode(binData);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-24-2023 11:54 AM
@Manish65 After I did that my output is nothing absolutely, FYI my attachment file is a txt file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-21-2024 10:33 PM
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: