Move Employee Document table record to Sys Attachment table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2024 02:26 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2024 04:40 AM
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
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2024 01:06 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 03:16 PM
Hello @Runjay Patel
I've tried your script with the few changes as per below,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2024 05:36 AM
@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