Create .txt file in sys_attachment from data stored in a table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2013 12:40 PM
Has anyone programatically created a text file attachment from data stored in a field in a ServiceNow table from a business rule?
I am essentially trying to emulate the behavior in the Attachment Note workflow activity. I need to create an attachment, but on a table that doesn't support that activity - so I want to include a Run Script activity with code that will allow me to create an attachment on the record.
I was thinking something like:
var file = new GlideRecord("sys_attachment");
file.file_name = "testname.txt";
file.CONTENTS = VARIABLECONTENT;
var sysID = rdp_file.insert();
But am not sure how to actually funnel the text from my variable into the content for the attachment record.
Any help would be appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2013 06:25 AM
For those that are interested, following is my solution:
insertAttachment(); //note, you need to wrap the StringUtil Package call (below) inside a function or this will error
function insertAttachment() {
var attachmentName = 'text.txt:plain/text';
var attachmentSource = 'cmdb_ci_ec2_instance:' + current.sys_id; //inserting this onto a specific EC2 VM instance - you can target your desired table accordingly
var attachmentPayload = 'some string';
var StringUtil = Packages.com.glide.util.StringUtil;
var encodedPayload = StringUtil.base64Encode(attachmentPayload);
var rec = new GlideRecord('ecc_queue');
rec.initialize();
rec.agent = "AttachmentCreator";
rec.topic = "AttachmentCreator";
rec.name = attachmentName;
rec.source = attachmentSource;
rec.payload = encodedPayload;
var flag = rec.insert();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2015 06:20 AM
Just wanted to thank you for this ... Saved my day
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2016 10:58 AM
So, this is a bit of an old thread, but super helpful. You can also do this without using the ecc_queue as well.
insertAttachment();
function insertAttachment() {
var attachmentPayload = 'some text';
i = new GlideRecord('incident')
i.get('bdb5c2a3dbc4ee0043563bc0cf961981')
var sa = new GlideSysAttachment();
var attachmentId = sa.write(i, 'test.txt', 'plain/text', attachmentPayload);
if(attachmentId)
return true;
else
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2017 08:20 AM
Nice one Daniel!!