Getting contents of an attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2013 01:34 PM
I need to get the contents of an attachment and have tried using the following in a background script (for testing purposes only):
var sa = new GlideSysAttachment();
var binData = sa.getBytes('incident', '892a8ba0a400f000e9ece890a93b7172');
gs.print(binData);
However the response I get back is always something like:
[B@bdbc52
Which doesn't look at all like the file I have attached. I've tried a few different file attachment types and attached them to a few different types of record.
I have also tried passing a only GlideRecord on the sys_attachment table to getBytes and I get the same answer.
Once I have the contents of the file, I'd like to modify the content and create a new attachment (on a different record) with my modified content.
It looks like all of this should be possible, but I just can't get past the garbage I get out of getBytes.
I'm on Calgary Patch 2 Hotfix 5.
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-28-2015 09:51 AM
Worked flawlessly! Thanks for posting!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2013 01:37 PM
Thank you for the swift help there.
The one thing I'm left with is how to now write that to a new attachment record.
Is there a GlideSysAttachment.setBytes(table_name, sys_id, byte_array)?
If there is, are the arguments similar to the getBytes?
I can probably manage the GlideRecord creation for the sys_attach.. record, or I might just do a GlideSysAttachment.copy. But I still need to be able to set the content of my new attachment.
Again - thank you.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2013 02:28 PM
Look at the SoapAttachments script include and then find this function createAttachmentFromAttachedEncodedString. I think this has the particular code that you're looking for.
Good luck!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2013 02:34 PM
Yes, that Script Include looks very helpful
Thanks very much
Matthew Pearson
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2013 12:25 PM
When I was originally trying to get the text from an attachment, I found exactly the same problem, if I tried to use the result of getBytes as if it were a string, I only got something like [B@143e157. So after hunting around I came up with the following:
var grAttach = new GlideRecord('sys_attachment');
grAttach.addQuery('table_sys_id', recordID);
grAttach.orderByDesc('sys_updated_on');
grAttach.query();
if ( grAttach.next() ) {
var attachID = grAttach.sys_id;
// Get the contents of the file
var sa = new GlideSysAttachment();
var binData = sa.getBytes(grAttach);
strData = Packages.java.lang.String(binData);
}
The important bit above is the part that Andrew points out. Do a getBytes on the GlideSysAttachment, then you have a binary data array. From the data array create a new string using the Java string conversion in Packages.java.lang.String.
There are some scripts in the system that use GlideSysAttachment, and you can use these as examples. They are also useful if you want to write or create an attachment too.