Parag Kanoje
Tera Contributor

Hello team,

Recently I was trying to archieve one requirement "copy attachments RITM level to TASK level and TASK level to RITM level without duplication. End your can see on portal what's happening on the task level". I tried to search answer on ServiceNow community but not get exact solution. After many trial Finally I found one solution we can do this using Business Rules.

You need you need to create 3 Business rules. 

1) sc_task = copy RITM level to TASK level(before - insert)
2) sc_task = copy TASK level to RITM level(before - insert/update)
3) sys_attachment = remove duplicate attachments(before - insert, update)


1) sc_task = copy RITM level to TASK level(before - insert) :
script : 
           (function executeRule(current, previous /*null when async*/ ) {
    var gr = new GlideRecord('sys_attachment');
    gr.addQuery("table_sys_id",current.sys_id);
    gr.query();
                        GlideSysAttachment.copy('sc_req_item', current.request_item, 'sc_task', current.sys_id);
    gs.info(current.number);
    
})(current, previous);

2) sc_task = copy TASK level to RITM level(after - update): 

script : 

(function executeRule(current, previous /*null when async*/ ) {
GlideSysAttachment.copy('sc_task', current.sys_id, 'sc_req_item', current.request_item);
 gs.info(current.number);
    
})(current, previous);

3) sys_attachment = remove duplicate attachments(before - insert, update) : 
script : 

(function executeRule(current, previous /*null when async*/ ) {

    var attach = new GlideRecord('sys_attachment');
    attach.addQuery('table_sys_id', current.table_sys_id);
    attach.addQuery('table_name', current.table_name);
    attach.addQuery('file_name', current.file_name);
    attach.addQuery('content_type', current.content_type);
    attach.addQuery('size_bytes', current.size_bytes);
    attach.query();
    if (attach.next()) {
        current.setAbortAction(true);
    }

})(current, previous);



 Hope this will solve your copy attachment issue.
If this works for you please feel free to mark Helpful

Comments
Thomas Buecker
Tera Contributor

Hi,

be cautious with copying attachments!

If you your company is at an enterprise level your attachment table will grow significantly, you can get performance issues and you will pay more money to ServiceNow as you consume more space in their data center.

There are other solutions available like a related list which contains links to the attachments of parent and child records.

suraj sharma999
Tera Contributor
you can do this by using two br only.
 1. sys_attachment == attchement attach vice varsa
(function executeRule(current, previous /*null when async*/ ) {
    var scTask = new GlideRecord('sc_task');
    if (current.table_name == 'sc_task') {
        if (scTask.get(current.table_sys_id.toString())) {
            GlideSysAttachment.copy('sc_task', scTask.sys_id.toString(), 'sc_req_item', scTask.request_item.sys_id.toString());
        }
    } else if (current.table_name == 'sc_req_item') {
        var scReqItem = new GlideRecord('sc_req_item');
        if (scReqItem.get(current.table_sys_id.toString())) {
            scTask.addQuery('request_item', scReqItem.sys_id.toString());
            scTask.query();
            while(scTask.next()) {
               GlideSysAttachment.copy('sc_req_item', scReqItem.sys_id.toString(), 'sc_task', scTask.sys_id.toString());
            }
        }
    }
})(current, previous);
 

2) sys_attachment = remove duplicate attachments(before - insert, update) : 
script : 

(function executeRule(current, previous /*null when async*/ ) {

    var attach = new GlideRecord('sys_attachment');
    attach.addQuery('table_sys_id', current.table_sys_id);
    attach.addQuery('table_name', current.table_name);
    attach.addQuery('file_name', current.file_name);
    attach.addQuery('content_type', current.content_type);
    attach.addQuery('size_bytes', current.size_bytes);
    attach.query();
    if (attach.next()) {
        current.setAbortAction(true);
    }

})(current, previous);

Version history
Last update:
‎07-07-2022 05:07 AM
Updated by: