- 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 09:21 PM
Hi Mussie,
There appears to be a couple issues with this code. The first is when you are performing the shift function on the array, this simply removes the first row from the array and does not return anything. Therefore, you should perform the shift function without attempting to set it to a new variable. Update the below lines:
17. csv_array.shift(); //removes the first row, edit file
19. csv_string = csv_array.join("\r\n");
Next, you will need to define the attachment variable by placing the below line before line 24.
var attachment = new Attachment();
Finally, the attachment.write function needs a couple tweaks. You will need to add the file extension to the name "1111.csv", and the document should be the data variable. See below for updates:
24. var attachment_sys_id = attachment.write("ecc_agent_attachment", table_sys_id, "1111.csv", "test/csv", data);
Hope this helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2021 01:48 AM
Hi,
Can you please help me with the code for a dynamic attachment in a notification?
Requirement is - User is selecting Request Type as Export Group Members and file should be sent to Requested For with the list of Group members in that particular group.
File varies from group to group. Please help me with the code for this.
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2017 09:49 PM
I am not sure if the encoding and decoding part is going to work for removing the header.
But one thing I can help you is that can remove the ecc queue from the equation and create the attachment directly as below.
var gr1 = new GlideRecord('incident');
gr1.addQuery('sys_id',table_sys_id);
gr1.query();
gr1.next();
var attachment = new GlideSysAttachment();
var attachment_sys_id = attachment.write(gr1, "1111.csv", "test/csv", data);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2017 10:20 PM
Thanks guys, I have made the changes you suggested. I know the GlideSysAttachment isn't working, so I ended up creating a script include as shown here. Here is my final code and I am able to attach the file but it isn't in a proper format. So my issue is to strip off the header column and attach it back in csv format, appreciate if you could have a second look and suggest the ideas you might have:
//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);
//identify the parent record to which the attachment should be attached
var gr = new GlideRecord('ecc_agent_attachment');
gr.get('c47d0b294f373200592d52411310c7ed');
//attach the attachment.
new CreateAttachmentExportSet(gr, "1111.csv", "test/csv", data);
}
Script include:
var CreateAttachmentExportSet = Class.create();
CreateAttachmentExportSet.prototype = {
initialize: function(record,name,contentType, data) {
var attCreator = new GlideRecord('ecc_queue');
attCreator.agent = "AttachmentCreator";
attCreator.topic = "AttachmentCreator";
//attCreator.name = name + ":" + contentType;
attCreator.name = name;
attCreator.source = record.getTableName()+":"+record.sys_id;
//attCreator.source = record;
attCreator.payload = data;
attCreator.insert();
},
type: 'CreateAttachmentExportSet'
};