Move Employee Document table record to Sys Attachment table

Kirankumar Nal1
Tera Contributor

Dear All,

 

Need to help in below,

 

Would like to move the existing attachments uploaded in Employee Document (sn_hr_ef_employee_document) table to sys_attachment table using script.

 

Please suggest me on this.

 

Thank you
 
Regards,
Kirankumar
5 REPLIES 5

Runjay Patel
Giga Sage

Hi @Kirankumar Nal1 ,

 

First of all you should not do this this will breach the security. Employee documents are stored in separate table because of security reason.

 

If you still wanna do then you can try below code

      var sourceGR = new GlideRecord('sn_hr_ef_employee_document');
    sourceGR.query();

    while (sourceGR.next()) {
        // Fetch attachments for the current record
        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_sys_id', sourceGR.sys_id);
        attachmentGR.addQuery('table_name', sourceTable);
        attachmentGR.query();

        while (attachmentGR.next()) {
            // Clone the attachment
            var newAttachment = new GlideRecord('sys_attachment');
            newAttachment.initialize();
            newAttachment.name = attachmentGR.name;
            newAttachment.content_type = attachmentGR.content_type;
            newAttachment.size_bytes = attachmentGR.size_bytes;
            newAttachment.table_sys_id = sourceGR.sys_id; // Keep the same parent
            newAttachment.table_name = 'sys_attachment'; // Target table
            var newAttachmentSysId = newAttachment.insert();

            // Copy attachment content (sys_attachment_doc)
            var attachmentDocGR = new GlideRecord('sys_attachment_doc');
            attachmentDocGR.addQuery('attachment', attachmentGR.sys_id);
            attachmentDocGR.query();

            while (attachmentDocGR.next()) {
                var newAttachmentDoc = new GlideRecord('sys_attachment_doc');
                newAttachmentDoc.initialize();
                newAttachmentDoc.attachment = newAttachmentSysId;
                newAttachmentDoc.position = attachmentDocGR.position;
                newAttachmentDoc.data = attachmentDocGR.data;
                newAttachmentDoc.insert();
            }

            // Optionally delete the original attachment
            // attachmentGR.deleteRecord();
        }
    }

    gs.print('Attachment migration completed!');

 

Try with one record first.

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Thank you Runjay for the script.

As per Michael below, it may requires to update the table and table sys id for the existing sys attachment record.

 

Thank you 

Hello @Runjay Patel 

 

I've tried your script with the few changes as per below,

 

var sysId = '7a7ea907837d12100e0d6ed0deaad3b0';
var sourceGR = new GlideRecord('sn_hr_ef_employee_document');
sourceGR.addEncodedQuery('sys_id=' + sysId);
sourceGR.query();

if (sourceGR.next()) {
    // Fetch attachments for the current record
    var attachmentGR = new GlideRecord('sys_attachment');
    attachmentGR.addQuery('table_sys_id', sourceGR.sys_id);
    attachmentGR.addQuery('table_name', 'sn_hr_ef_employee_document');
    attachmentGR.query();

    if (attachmentGR.next()) {
        // Clone the attachment
        var newAttachment = new GlideRecord('sys_attachment');
        newAttachment.initialize();
        newAttachment.name = attachmentGR.name;
        newAttachment.content_type = attachmentGR.content_type;
        newAttachment.size_bytes = attachmentGR.size_bytes;
        newAttachment.table_sys_id = sourceGR.sys_id; // Keep the same parent
        newAttachment.table_name = 'sn_hr_core_case'; // Target table
        var newAttachmentSysId = newAttachment.insert();

        // Copy attachment content (sys_attachment_doc)
        var attachmentDocGR = new GlideRecord('sys_attachment_doc');
        attachmentDocGR.addQuery('attachment', attachmentGR.sys_id);
        attachmentDocGR.query();

        while (attachmentDocGR.next()) {
            var newAttachmentDoc = new GlideRecord('sys_attachment_doc');
            newAttachmentDoc.initialize();
            newAttachmentDoc.attachment = newAttachmentSysId;
            newAttachmentDoc.position = attachmentDocGR.position;
            newAttachmentDoc.data = attachmentDocGR.data;
            newAttachmentDoc.insert();
        }

        // Optionally delete the original attachment
        // attachmentGR.deleteRecord();
    }
}
 
But this is nor creating a new record in sys_attachment table.
Any suggestion on this please.
 
Thank you
 
Regards,
Kiran

michaelj_sherid
ServiceNow Employee
ServiceNow Employee

@Kirankumar Nal1 The Employee documents already reside in the Attachments table. The Employee doc refers back to this record and the (Document Type) security is applied once the Employee Document record is created. The same applies to the documents that are "moved" from a HR Case to the Employee document, the attachment still resides in sys_attachment, but is just flagged as such.

If my answer has helped with your question, please mark my answer as an accepted solution and give a thumbs up.

Regards,

Mike