Is there any way to copy single attachment from multiple attachments ??

Satya8
Mega Expert

Hello guys,

Is there any way to copy single attachment from multiple attachments ??. suppose i have multiple attachments on ritm i have to copy only one from them and add it to an catalog task. Please help me how to achieve this..

Thanks

7 REPLIES 7

Ankush16
Kilo Guru

how you are differentiating different files based on name  ?

You can use below function in business rule 

GlideSysAttachment.copy('sc_req_item',sysidofrecord,'sc_task',sys_idofrecord);

 

(String sourceTable, String sourceID, String targetTable, String targetID)

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Satya,

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

For example purpose I have used file name as "Hello.xls"; you can use yours; also ensure proper RITM sys id and SC Task sys id you give before testing this

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
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

By using above script attachment getting copied but when after downloading attachment it shows error content not visible?

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Satya,

Any update on this?
Can you mark my answer as correct, 👍 helpful if you were able to achieve the requirement & then close the thread. Thanks in advance.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader