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

vkachineni
Kilo Sage
Kilo Sage
FillClientDocumentFileAttachments();
function FillClientDocumentFileAttachments()
{
	var base64dataCSV = 'VGhpcyBpcyBhIFRlc3QgQ1NWIGZpbGUNCg==';  //CSV as BASE64
	var decodedBytesCSV = GlideStringUtil.base64DecodeAsBytes(base64dataCSV);

	var attachGR = new GlideRecord('sys_attachment');
	attachGR.addQuery('table_name', "table_name");
	attachGR.addQuery('table_sys_id', "table_sys_id");
	attachGR.query();		
	if(!attachGR.next())
	{
		var sa = new GlideSysAttachment();
		sa.write(clientDocFileGR, clientDocFileGR.u_file_name, 'application/csv',  decodedBytesCSV);		
	}
	gs.info("Completed adding attachments.");
}

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

tobrien
Kilo Guru

I credit all of you with workable solutions, and the only reason I marked Mr. Singh as "CORRECT" is because of its' succinctness.

Thanks!