Create Attachments using GlideSysAttachment()

Mussie
ServiceNow Employee
ServiceNow Employee

Hi Guys,

I would like to get an attachment from sys_attachment table and edit it and re-attach it as a different file. I have the below code, but it isn't working? Can you please check what is missing?

//fetch the attachment file that needs to be edited

var attachmentGR = new GlideRecord("sys_attachment");

attachmentGR.addEncodedQuery('GOTOtable_name=ecc_agent_attachment^content_type=test/csv^file_nameSTARTSWITH1');

attachmentGR.orderByDesc('sys_created_on');

attachmentGR.query();

if(attachmentGR.next()){

var table_sys_id = attachmentGR.table_sys_id;

// read the attachment...

var stringUtil = new GlideStringUtil();

var sa = new GlideSysAttachment();

var binData = sa.getBytes(attachmentGR);

// convert it to Encoded Data..

var encData = stringUtil.base64Encode(binData);

// decode the encoded data into string..

var csv_string = stringUtil.base64Decode(encData) + '';

var csv_array = csv_string.split("\r\n");

var new_csv_array = csv_array.shift(); //removes the first row, edit file

// convert this back into a CSV string...

csv_string = new_csv_array.join("\r\n");

// attach it back...

var base64EncodeString = stringUtil.base64Encode(csv_string);

var data = stringUtil.base64DecodeAsBytes(this.base64EncodeString);

//attach the attachment.

var attachment_sys_id = attachment.write("ecc_agent_attachment", table_sys_id, "1111", "test/csv", document);

}

Mussie

1 ACCEPTED SOLUTION

Hi Mussie,



The original code should work with the changes I suggested.   Try this:



//fetch the attachment file that needs to be edited  


var attachmentGR = new GlideRecord("sys_attachment");  


attachmentGR.addEncodedQuery('GOTOtable_name=ecc_agent_attachment^content_type=test/csv^file_nameSTARTSWITH1');  


attachmentGR.orderByDesc('sys_created_on');  


attachmentGR.query();  




if(attachmentGR.next()){  


  var table_sys_id = attachmentGR.table_sys_id;  


  // read the attachment...  


  var stringUtil = new GlideStringUtil();  


  var sa = new GlideSysAttachment();  


  var binData = sa.getBytes(attachmentGR);  


  // convert it to Encoded Data..  


  var encData = stringUtil.base64Encode(binData);  


  // decode the encoded data into string..  


  var csv_string = stringUtil.base64Decode(encData) + '';  


  var csv_array = csv_string.split("\r\n");


  csv_array.shift(); //removes the first row, edit file  


  // convert this back into a CSV string...  


  csv_string = csv_array.join("\r\n");


  // attach it back...  


  var base64EncodeString = stringUtil.base64Encode(csv_string);  


  var data = stringUtil.base64DecodeAsBytes(this.base64EncodeString);  


  //attach the attachment.


  var attachment = new Attachment();  


  var attachment_sys_id = attachment.write("ecc_agent_attachment", table_sys_id, "1111.csv", "test/csv", data);  


}  


View solution in original post

10 REPLIES 10

Hi Mussie,



The original code should work with the changes I suggested.   Try this:



//fetch the attachment file that needs to be edited  


var attachmentGR = new GlideRecord("sys_attachment");  


attachmentGR.addEncodedQuery('GOTOtable_name=ecc_agent_attachment^content_type=test/csv^file_nameSTARTSWITH1');  


attachmentGR.orderByDesc('sys_created_on');  


attachmentGR.query();  




if(attachmentGR.next()){  


  var table_sys_id = attachmentGR.table_sys_id;  


  // read the attachment...  


  var stringUtil = new GlideStringUtil();  


  var sa = new GlideSysAttachment();  


  var binData = sa.getBytes(attachmentGR);  


  // convert it to Encoded Data..  


  var encData = stringUtil.base64Encode(binData);  


  // decode the encoded data into string..  


  var csv_string = stringUtil.base64Decode(encData) + '';  


  var csv_array = csv_string.split("\r\n");


  csv_array.shift(); //removes the first row, edit file  


  // convert this back into a CSV string...  


  csv_string = csv_array.join("\r\n");


  // attach it back...  


  var base64EncodeString = stringUtil.base64Encode(csv_string);  


  var data = stringUtil.base64DecodeAsBytes(this.base64EncodeString);  


  //attach the attachment.


  var attachment = new Attachment();  


  var attachment_sys_id = attachment.write("ecc_agent_attachment", table_sys_id, "1111.csv", "test/csv", data);  


}  


I can confirm that this works.


Thank you very much guys, this is now working but I have another problem now. The file is not getting transferred to the MID server. I will post another question for that.


Hi Guys,


Can you check this post and see if you can help?


How to send attachment files to MID Server


Mussie


 

Hi Kalai,

Can we not add data in the next line instead of the same row all together?

or what if we want to push data in two columns and next line format?