How to copy single attachment from the multiple attachment to send to target table

chandan31
Tera Contributor

Hi all,

 

Hoe to copy the single attachment from the multiple attachment by using sys_attachment table that single attachment i need to attach child table .

 

Can you give me the BR script

1 ACCEPTED SOLUTION

Harsh_Deep
Giga Sage
Giga Sage

Hello @chandan31 ,

GlideSysAttachment.copy will copy all the attachments from source to target

if you want to copy single attachment then use below script

I assume you want to attach single file based on file name; so I have applied query as table + record sys id + file name

Please use this code to achive your requirement 

copySingleAttachment();

function copySingleAttachment(){

var attRec;
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'sc_req_item');
gr.addQuery('table_sys_id','<RitmSysId>');
gr.addQuery('file_name', 'Hello.xls');
gr.query();
if(gr.next()){
var gr1 = new GlideRecord('sys_attachment');
gr1.initialize();
gr1.file_name = gr.file_name;
gr1.content_type = gr.content_type;
gr1.compressed = gr.compressed;
gr1.table_name = 'sc_task';
gr1.size_bytes = gr.size_bytes;
gr1.size_compressed = gr.size_compressed;
gr1.table_sys_id = <targetTaskRecordSysId>
attRec = gr1.insert();
}

var attDoc = new GlideRecord('sys_attachment_doc');
attDoc.addQuery('sys_attachment', gr.sys_id);
attDoc.query();
while(attDoc.next()){
var attDocCopy = new GlideRecord('sys_attachment_doc');
attDocCopy.initialize();
attDocCopy.sys_attachment = attRec;
attDocCopy.position = attDoc.position;
attDocCopy.length = attDoc.length;
attDocCopy.data = attDoc.data;
attDocCopy.insert();
}

}

 Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

Thanks

View solution in original post

2 REPLIES 2

Harsh_Deep
Giga Sage
Giga Sage

Hello @chandan31 ,

GlideSysAttachment.copy will copy all the attachments from source to target

if you want to copy single attachment then use below script

I assume you want to attach single file based on file name; so I have applied query as table + record sys id + file name

Please use this code to achive your requirement 

copySingleAttachment();

function copySingleAttachment(){

var attRec;
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'sc_req_item');
gr.addQuery('table_sys_id','<RitmSysId>');
gr.addQuery('file_name', 'Hello.xls');
gr.query();
if(gr.next()){
var gr1 = new GlideRecord('sys_attachment');
gr1.initialize();
gr1.file_name = gr.file_name;
gr1.content_type = gr.content_type;
gr1.compressed = gr.compressed;
gr1.table_name = 'sc_task';
gr1.size_bytes = gr.size_bytes;
gr1.size_compressed = gr.size_compressed;
gr1.table_sys_id = <targetTaskRecordSysId>
attRec = gr1.insert();
}

var attDoc = new GlideRecord('sys_attachment_doc');
attDoc.addQuery('sys_attachment', gr.sys_id);
attDoc.query();
while(attDoc.next()){
var attDocCopy = new GlideRecord('sys_attachment_doc');
attDocCopy.initialize();
attDocCopy.sys_attachment = attRec;
attDocCopy.position = attDoc.position;
attDocCopy.length = attDoc.length;
attDocCopy.data = attDoc.data;
attDocCopy.insert();
}

}

 Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

Thanks

aidanmtx
Tera Contributor

You may want to consider using the writeContentStream (see documentation)  function of the GlideSysAttachment class. You can call getContentStream on the attachment you want to copy, and feed that to the writeContentStream. The following example was adapted from the example under the writeContentStream documentation. I used this function successfully:

 

 

// srcAtt: GlideRecord pointing to the sys_attachment to copy
// dstRec: GlideRecord you want to copy the attachment to
function copyAttachment(srcAtt, dstRec) {
	var gsa = new GlideSysAttachment();
	return gsa.writeContentStream(
		dstRec,
		srcAtt.getValue('file_name'),
		srcAtt.getValue('content_type'),
		gsa.getContentStream(srcAtt.getValue('sys_id'))
	);
}