pr8172510
Kilo Sage

Hi @ronro2,

Business Rule setup:

  • Table: Incident (or your target table)

  • When: After

  • Insert: Checked

  • Condition: (leave empty)

    (function executeRule(current, previous) {

        // Get the attachment variable value
        var attachmentVar = current.variables.bifoga_fil;
       
        if (!attachmentVar) {
            gs.info('No attachment found in bifoga_fil variable');
            return;
        }
       
        gs.info('Found attachment sys_id: ' + attachmentVar);
       
        // Method 1: Direct copy using GlideSysAttachment
        var sourceGr = new GlideRecord('sys_attachment');
        if (sourceGr.get(attachmentVar)) {
           
            var destTable = current.getTableName();
            var destSysId = current.getUniqueValue();
           
            gs.info('Copying to: ' + destTable + ' / ' + destSysId);
           
            // Create new attachment record
            var newAttach = new GlideRecord('sys_attachment');
            newAttach.initialize();
            newAttach.table_name = destTable;
            newAttach.table_sys_id = destSysId;
            newAttach.file_name = sourceGr.file_name;
            newAttach.content_type = sourceGr.content_type;
            newAttach.size_bytes = sourceGr.size_bytes;
            var attachId = newAttach.insert();
           
            // Copy the actual file data
            var sourceDoc = new GlideRecord('sys_attachment_doc');
            sourceDoc.addQuery('sys_attachment', attachmentVar);
            sourceDoc.query();
           
            while (sourceDoc.next()) {
                var newDoc = new GlideRecord('sys_attachment_doc');
                newDoc.initialize();
                newDoc.sys_attachment = attachId;
                newDoc.data = sourceDoc.data;
                newDoc.insert();
            }
           
            gs.info('Attachment copied successfully!');
        }

    })(current, previous);


    pr8172510_0-1777028388227.png

     

    pr8172510_1-1777028414479.png