Copying the Attachments from REQ to RITM to SC Task

Ganesh Manasali
Tera Contributor

Hi All,
I have a requirement to copy the attachments as below

 

1. IF attachment added to Req then it should copy to RITM and SC TASK

2. If attachment added to RITM then it should copy to SC TASK

 

To achieve this I have created a insert BR on sys_attachment table

Condition: Table name is sc_req_item or sc_request

 

Script:

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

    if (current.table_name == 'sc_request') {
        gs.log('REQ ganesh');
        var requestSysId = current.table_sys_id;
        var gr = new GlideRecord('sc_req_item');
        gr.addQuery('request', requestSysId);     
        gr.query();
        while (gr.next()) {
            var att = new GlideRecord('sys_attachment');
            att.initialize();
            att.file_name = current.file_name;
            att.size_bytes = current.size_bytes;
            att.table_name = 'sc_req_item';
            att.table_sys_id = gr.sys_id;
            att.image_height = current.image_height;
            att.image_width = current.image_width;
            att.content_type = current.content_type;
            att.compressed = current.compressed;
            att.average_image_color = current.average_image_color;
            gs.log('kishh' + current.average_image_color);
            att.size_compressed = current.size_compressed;
            att.chunk_size_bytes = current.chunk_size_bytes;
            att.hash = current.hash;
            var attRec = att.insert();
            var attDoc = new GlideRecord('sys_attachment_doc');
            attDoc.addQuery('sys_attachment', current.sys_id);
            //attDoc.setWorkflow(false);
            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();
            }
        }
    }

    if (current.table_name == 'sc_req_item') {
        gs.log('RITM ganesh');
        var ritmSysId = current.table_sys_id; // RITM sys id
        var gr2 = new GlideRecord('sc_task');
        gr2.addQuery('request_item', ritmSysId);
        gr2.query();
        while (gr2.next()) {
            var att1 = new GlideRecord('sys_attachment');
            att1.initialize();
            att1.file_name = current.file_name;
            att1.content_type = current.content_type;
            att1.compressed = current.compressed;
            att1.table_name = 'sc_task';
            att1.size_bytes = current.size_bytes;
            att1.size_compressed = current.size_compressed;
            att1.table_sys_id = gr2.sys_id;
            att1.size_compressed = current.size_compressed;
            att1.chunk_size_bytes = current.chunk_size_bytes;
            att1.hash = current.hash;
            var attRec1 = att1.insert();
            //check whether current record present in sys_attachment_doc
            var attDoc1 = new GlideRecord('sys_attachment_doc');
            attDoc1.addQuery('sys_attachment', current.sys_id);
            attDoc1.query();
            //If current record present in sys_attachment_doc copy the data to sc_task attachment.
            while (attDoc1.next()) {
                var attDocCopy1 = new GlideRecord('sys_attachment_doc');
                attDocCopy1.initialize();
                attDocCopy1.sys_attachment = attRec1;
                attDocCopy1.position = attDoc1.position;
                attDocCopy1.length = attDoc1.length;
                attDocCopy1.data = attDoc1.data;
                attDocCopy1.insert();
            }
        }
    }


})(current, previous);

 

When I add attachment on RITM table then it will copy to SC Task as expected

but when I add attachment to REQ then attachment will copy to RITM and SC Task but while opening the attachment on sc task its thwoing error like file corrupted. 

Can anyone suggest me on this. Thanks in advance!

 

Regards,

Ganesh

5 REPLIES 5

Community Alums
Not applicable

Hi All,
I was able to achieve this functionality using the OOB flow action 'Copy attachment'.

Created a flow on attachment table when an attachment inserted and table is sc_request

then using the action copy attachment, configured the flow to copy the specific attachment. 

Similarly created another flow for sc_req_item.

 

Instead of script, this method is good and easy to use. 

 

Thank You!

 

Regards,

Ganesh