Any get GlideSysAttachment.write to work for binary files while in a scope?

reedwowens
Giga Expert

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!

1 ACCEPTED SOLUTION

reedwowens
Giga Expert

I've got this in as a bug to ServiceNow.   They are aware of the issue and looking at fixing it in a future release.   For now GlideSysAttachment().write() works only for text files and is not binary safe.   Problem


View solution in original post

19 REPLIES 19

Call me on my cell


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?


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.


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.

jarieskelinen
Kilo Contributor

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();