
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2016 08:25 PM
I have been bashing my head against this and can't believe I'm the only one to run into this. Hope it's just I'm doing something wrong.
To test problem I created a simple form with an encoded data field and a UI action:
var sa = new GlideSysAttachment();
var binData = gs.base64Decode(current.encoded_contents);
sa.write(current, "Test.pdf", "application/pdf",binData);
action.setRedirectURL(current);
You would think this works... NOPE. I get an attachment. It thinks it's PDF and it's about 20% larger than the original. All the ascii text is correct in it but the binary is messed up.
I've tested the encode/decode -> mid server (did it in java) -> server... All working but this write function.
Developer site has only the description of the call and it's function. No examples. From my "working with it' it does take the text and write it to the attachment..
Any help/insight would be greatly appreciated!
Just tried another obvious thing. Added a PDF as an attachment to this record and ran the following UI Action:
var gr = new GlideRecord("sys_attachment");
gr.addQuery( "table_name", "=", "x_uxs_test_error_table");
gr.addQuery( "table_sys_id", "=" , current.sys_id + "");
gr.query();
if (gr.next()) {
gs.addInfoMessage("Getting attachment");
var sa = new GlideSysAttachment();
var contents= sa.getContentBase64(gr);
gs.addInfoMessage("Got Contents: ");
current.encoded_contents = contents;
current.update();
sa.write(current, "newOrig.pdf","application/pdf", gs.base64Decode(contents));
}
action.setRedirectURL(current);
I get the infomessages. I get the data in base64 encoded on the form. I get a second "newOrig.pdf" and it's wrong!
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2016 02:40 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2016 09:45 AM
Call me on my cell
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2016 06:43 AM
Hi Reed,
I'm also having this same issue while running a scoped app. I'm running a Geneva instance. Is there something I can do to make it work?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2016 06:58 AM
Jacobo,
Look at Jan's response below. This is currently the only way to do it on ServiceNow. I am told it will be fixed in Istanbul, but as we support Fuji+, we will be using this work-a-round for awhile.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2024 10:32 AM
This is just an update for history. The bug is still present as of June 2024. I submitted a support ticket, but they refused to fix it. I'm looking into a workaround with "GlideSysAttachment().writeBase64", and so far it looks promising. And, yes, you must implement "bytesToBase64" on your own because it is not available in the scoped app.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2016 03:46 AM
Problem lies within gs.base64Decode() function which returns a String instead of a Byte array which GlideSysAttachment.write() expects. There is no documented API call to decode base64 encoded data to byte array or if there is please let me know.
However, you can go old-school and work yourself around this problem by using ecc_queue and AttachmentCreator which accepts base64 encoded data as is:
var eccGr = new GlideRecord('ecc_queue');
eccGr.initialize();
eccGr.setValue('agent', 'AttachmentCreator');
eccGr.setValue('topic', 'AttachmentCreator');
eccGr.setValue('name', fileName+':'+contentType);
eccGr.setValue('source', tableName+':'+tableSysId);
eccGr.setValue('payload', content);
eccGr.insert();