Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Getting contents of an attachment

MatthewPearson
Tera Contributor

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

20 REPLIES 20

Glad you sorted it out. Although it is interesting you say GlideStringUtil didn't work for you.  Previously I have used this to retrieve attachments and base64 encode for building EML files:

var attBytes = (new GlideSysAttachment()).getBytes(grAttach);
var base64EncData = GlideStringUtil.base64Encode(attBytes);

Thanks for your solution @régissarlé