- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2017 08:32 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2017 10:42 PM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2017 10:42 PM
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);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2017 10:47 PM
I can confirm that this works.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2017 05:52 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2017 08:06 PM
Hi Guys,
Can you check this post and see if you can help?
How to send attachment files to MID Server
Mussie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2018 09:29 AM
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?