Copied attachments are not opening properly on SC task from RITM

Ganesh Manasali
Tera Contributor

Hi All,

 

I have written an After BR on the attachment table to copy the attachments from RITM to SC Task. Attachment is getting copied but the attachment is not opening on the sc task. Below is the code I have used and kindly suggest me on this. Thank you!

 

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

    // Add your code here
    var ritmSysId = current.table_sys_id;
    var gr2 = new GlideRecord('sc_task');
    gr2.addQuery('request_item', ritmSysId);
    gr2.query();
    while (gr2.next()) {
        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 = 'sc_task';
        att.size_bytes = current.size_bytes;
        att.size_compressed = current.size_compressed;
        att.table_sys_id = gr2.sys_id;
        att.size_compressed = current.size_compressed;
        att.chunk_size_bytes = current.chunk_size_bytes;
        att.hash = current.hash;
        var attRec = att.insert();
        //check whether current record present in sys_attachment_doc
        var attDoc = new GlideRecord('sys_attachment_doc');
        attDoc.addQuery('sys_attachment', current.sys_id);
        attDoc.query();
        //If current record present in sys_attachment_doc copy the data to sc_task attachment.
        if (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);
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

It is recommended to not copy attachments whenever possible.  In this case you could add a related list to the Catalog Task form that shows the attachments of the related RITM.

 

When absolutely necessary, and at the risk of bloating your database, use the GlideSysAttachment API to copy attachments.

https://docs.servicenow.com/bundle/vancouver-api-reference/page/app-store/dev_portal/API_reference/G... 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

It is recommended to not copy attachments whenever possible.  In this case you could add a related list to the Catalog Task form that shows the attachments of the related RITM.

 

When absolutely necessary, and at the risk of bloating your database, use the GlideSysAttachment API to copy attachments.

https://docs.servicenow.com/bundle/vancouver-api-reference/page/app-store/dev_portal/API_reference/G... 

Jaspal Singh
Mega Patron
Mega Patron

Hi Ganesh,

 

As suggesed by Brad, recommened is to avoid duplicating the attachments. Why not refer it as a related list that simply leverages the relationship. Refer link