Is there any way to copy single attachment from multiple attachments ??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2019 09:41 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2019 09:50 PM
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2019 10:00 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2020 04:12 AM
By using above script attachment getting copied but when after downloading attachment it shows error content not visible?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2019 12:21 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader