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

Alikutty A
Tera Sage

Hi,



getBytes have limitation of 5 MB, Please read these posts and see if it helps you. There are alternate methods explained.



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


Reading attachment data and entering into a table



Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


Hi Alikutty,



Thanks for Reply.I have seen this post earlier and made the changes suggested in above two posts but none of this works.Can you please suggest working approach.



Thanks,


Lokesh.


Hi Alikutty, Thanks for the above post.Above post helps in resolving my Issue.Using below lines it works. //Read in attachment //Replaces standard method which is limited to 5mb //GlideSysAttachment(grAttachment.sys_id).getBytes(); var gsa = GlideSysAttachmentInputStream(grAttachment.sys_id.toString()); var baos = new Packages.java.io.ByteArrayOutputStream(); gsa.writeTo(baos); baos.close(); But,I am getting below error for files greater than 10MB.Can you please help in Resolving this:


I have post this issue description also attached the script I use in the link(https://community.servicenow.com/message/1123245#1123245) Thanks, Lokesh.


Khozema Attar1
Tera Guru

Hi Lokesh,



If you are still facing the issues, you can use the below code. This code works fine for us and we are able to send attachments of any size from ServiceNow.



var sa = GlideSysAttachment();


var binData = sa.getBytes(current); //referes to the current table in our case (sys_attachment)


var encData = GlideStringUtil.base64Encode(binData);


s.setStringParameter('attachment.filename',current.file_name);


              s.setStringParameter('attachment.type','Base64');


s.setStringParameter('attachment.data',encData);



Thanks,