Copy attachments from one table to other table in ServiceNow?

This post will help you with the script to copy all attachments & copy latest attachment from case table to change request table with the help of Business Rule in ServiceNow.

(function executeRule(current, previous /null when async/) {
// Copy attachment from CASE to CHANGE REQUEST
var csmAttach = new GlideRecord('change_request');
csmAttach.addQuery('u_case.sys_id', current.table_sys_id);
csmAttach.query();
if(csmAttach.next()){
var grattach = new GlideRecord('sys_attachment');
grattach.addQuery('table_sys_id', csmAttach.sys_id);
GlideSysAttachment.copy('x_infpr_csm_case', current.table_sys_id, 'change_request', csmAttach.sys_id);
//csmAttach.setWorkflow(false);
csmAttach.insert();
}
})(current, previous);

Below program will help you Copy latest attachment from CASE table to Change request table in ServiceNow using Business Rule?

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

var id = current.table_sys_id;
var grcase = new GlideRecord('x_infpr_csm_case');
grcase.addQuery('sys_id', id);
grcase.query();
if(grcase.next()){
    var chg = grcase.change;
    //Attachment Table
    var att = new GlideRecord('sys_attachment');
    att.initialize();
    att.file_name = current.file_name;
    att.content_type = current.content_type;
    att.compressed = current.compressed;
    att.table_name = 'change_request';
    att.size_bytes = current.size_bytes;
    att.size_compressed = current.size_compressed;
    att.table_sys_id = chg;
    var attRec = att.insert();
    //Attachment Document Table
    var attDoc = new GlideRecord('sys_attachment_doc');
    attDoc.addQuery('sys_attachment', current.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();
    }
}
})(current, previous);

Leave a Reply

Your email address will not be published. Required fields are marked *