Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

GlideSysAttachment().getBytes() is not working if size of attachment is more than 5 MB?

lvakare
Kilo Contributor

Hi All,

I am using Helsinki and I am facing same Issue When file size is greater than 5 MB showing as 0 bytes.Debugging this, at the line var binData = sa.getBytes(att), binData.length = 0.In ServiceNow community we see similar post(https://community.servicenow.com/thread/195363) which says getBytes() method has a limit of 5MB I tried solution given in the post(https://community.servicenow.com/thread/195363) but it doesn't work.Can any one help in resolving this.

Below is part of script I use:

function sendAttachment(att,counter) {

  gs.log('Export Attachments Script - Send Attachment #'+counter+' - START');

  var bool = false;

  try {

  //Create GlideSysAttachment object

  var sa = new GlideSysAttachment();

//Gets the binary data of the passed attachment

var binData = sa.getBytes(att);

//Encodes the binary to base64

  var encData = GlideStringUtil.base64Encode(binData);

//Prepares a file name a "TABLE_NAME - (commented)TICKET_SYS_ID - SYS_ID - FILE_NAME"

  var file_name = att.table_name + "-" + att.table_sys_id + "-" + att.sys_id + "-" +   sanitizeFileName(att.file_name.toString()).trim();

  //Calls send Request for binary data and generated file name

  sendRequest(encData, file_name);

  //If no exception always return true

  bool = true;

  } catch (Exception) {

  gs.log('Failed sending Attachment to due to Exception: '

  + Exception);

  }

  return bool;

}

5 REPLIES 5

ersureshbe
Giga Sage

Hi Kozhema,

I'm having an issue with base64 conversion from sys_attachment table. I have used below code. It contins limitation as 5 MB and It's not working more than 5 MB file. 

var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', current.sys_id);
gr.query();
if (gr.next())
{
var encData = new GlideSysAttachment().getContentBase64(gr);
return encData;
}
 
I hav tried to overcome this issue using getContentStream. But it's not working as expected.
 
var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', current.sys_id);
gr.query();
if (gr.next()){
 var inputStream = new GlideSysAttachment().getContentStream(gr.sys_id);
var digest = new GlideDigest();
var encData = digest.getSHA1Base64FromInputStream(inputStream);
return encData;
}
 
Can you please help to identify the issue from above code or share altrenate solution to fix base64 conversion issue.
 
Regards,
Suresh Loganathan.
Regards,
Suresh.