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

thanks, works as a charm!

Hi, 

When I tried above logic, it is adding an attachment but unable to open it.

reedwowens
Giga Expert

I've run into this problem if the attachment extension is not set and ServiceNow is setup to only allow certain extensions.   Try doing another attachment type and setting the name with the extension.   For instance -- MyWordDocument.docx or MyPic.jpg


shuguang2
Kilo Expert

Hi 

I would like to use GlideSysAttachment().write() to copy some attachment  from one Incident record to another. The GlideSysAttachment().copy() function copies all attachments, which is not our usecase. Therefore I need to first select the attachment on the source record, and then write needed ones to the source record.

Now what I found is that if I run the script, the GlideSysAttachment().write() function doesn't seem to work at all. It doesn't gives any error, but it doesn't create the attachment either. Below is the background script I used to check the function, it reads all attachment from Incident with sys_id a457f01fdb10230040640181ca9619b5 and checks whether it is attached less then 50min ago. If yes the wirte() function should copy it to the Incident with sys_id c777bc53db10230040640181ca96194a.

Every part works, the script finds all attachments and does the correct time compare, but when it comes to write it to the other record. Nothing happens.

I am on Jarkata release.

Any suggestion welcome.

**************

var attach = new GlideSysAttachment();
var gr = attach.getAttachments('incident', 'a457f01fdb10230040640181ca9619b5');
gr.addQuery('sys_updated_by', gs.getUserName());
var currentTime = new GlideDateTime();
currentTime.setValue(gs.nowDateTime());
currentTime.addSeconds(-3000); //50 minutes ago compare to current time
gs.print(currentTime.getValue());
gr.query();while(gr.next()){
var attachTime = new GlideDateTime();
attachTime.setValue(gr.sys_updated_on.getDisplayValue());
gs.print(attachTime.getValue());
if (currentTime.getValue() < attachTime.getValue())
{
attach.writeContent('c777bc53db10230040640181ca96194a', gr.file_name, gr.content_type, attach.getContent(gr));
}
}

 

 

Vanjuli
Giga Contributor

Hi folks,

I have been trying to convert text to base 64 encoded string that can be passed to writeBase64(GlideRecord now_GR, String fileName, String contentType, String content_base64Encoded) function of GlideSysAttachment API.

It seems there's not much documentation for stringUtil.base64Encode() or gs.base64Encode() function to convert text to base64 encoded format used in GlideSysAttachment API functions.

I am working with NewYork release and it seems even after encoding the plain text is returned instead of base 64 encoded.

I have checked multiple community articles but can't find appropriate function which may directly convert plain text to base 64 encoded text which may be passed to writebase64()  function of GlideSysAttachment API.