Is there a way to insert into the sys_attachment table directly (from say, a script) ?

tobrien
Kilo Guru

Hi,

I see that you can add a new attachment record using the GlideSysAttachment.write()...

But is it possible to insert directly into that table from a script include?

The sys_attachment table does not seem to have a "content" column.

e.g. ==>

 

var sRec = new GlideRecord('sys_attachment');

sRec.initialize();

sRec.setValue('filename', 'my.csv');

sRec.setValue('sizebytes', {the size of the csv file});

sRec.setValue( {where ever the content is}, {the csv data});

 

sRec.insert();

 

 

 

1 ACCEPTED SOLUTION

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Try using something as below. If required execute it in background script & then check sys_attachment table.

ar attachtable=new GlideRecord('sys_attachment');
attachtable.initialize();
var attachfile = new GlideSysAttachment();   
attachfile.write(attachtable, 'abcd.xls','test/csv', 'abcdsample');

 

Thanks,

Jaspal Singh

View solution in original post

6 REPLIES 6

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Try using something as below. If required execute it in background script & then check sys_attachment table.

ar attachtable=new GlideRecord('sys_attachment');
attachtable.initialize();
var attachfile = new GlideSysAttachment();   
attachfile.write(attachtable, 'abcd.xls','test/csv', 'abcdsample');

 

Thanks,

Jaspal Singh

how to add pdf in sys_attachment table if i have URL of pdf

 

can you insert an image by using this script

Anil Shewale
Mega Guru

Hi tobrien

You can upload attachment in sys_attachment table with the help of GlideSysAttachment.copy

 Example:

var sourceSysID = '1db6b56e6fa58f4882d1cf164b3ee4bf';
var targetSysID = current.sys_id;
var copyAtt = new GlideSysAttachment();
copyAtt.copy('kb_knowledge', sourceSysID, 'sc_req_item', targetSysID);

Here is the script I used in workflow to attach files in Service Catalog Request Items. Modify this script for your tables and see if it works. This is not the ideal way but a workaround I found to do it after trying so many other ways.

More info here Copy Attachments from Record to Record - ServiceNow Wiki

 

If it help mark helpful or correct 

Thanks and regards

Anil