- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 11:41 PM
We have multiple 'File attachment' type fields on XXX table. At the backend in sys_attachment table all such records are stored as "ZZ_YY"+current table name.
How does the system identify that this attachment is specific to this file attachment field?
How to Copy only one attachment in YYY field 'File attachment' to YYY another table?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2022 06:51 AM
Hello,
The "file attachment field" (in the example below named u_current_file) contains the sys_id from the sys_attachment table.
You can use a code like the one below to copy it.
var gr = new GlideRecord('xxxx');
gr.get('f847adac1b40d110be52419fe54bcbca');
var fSys = gr.u_current_file.toString();
gs.info("File sys id: " + fSys);
var gAtach = new GlideRecord('sys_attachment');
gAtach.get(fSys);
gs.info("File Name:" + gAtach.file_name);
var targetRef = new GlideRecord('your_table');
targetRef.get('target_sys_id');
//Example of script
var glideSysAttachmentRef = new GlideSysAttachment();
var guid = "";
var newFileName = gAtach.getValue("file_name");
guid = glideSysAttachmentRef.writeContentStream(targetRef, newFileName, gAtach.getValue("content_type"), glideSysAttachmentRef.getContentStream(gAtach.getUniqueValue()));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2022 02:41 AM
I haven't executed the script but putting everything together.
function getAttachmentSysId(rtim, fieldName) {
var grVo = new GlideRecord('sc_item_option_mtom');
grVo.addQuery('request_item.number', rtim);
grVo.addQuery('sc_item_option.item_option_new.name', fieldName);
grVo.query();
if (grVo.next()) {
gs.info('found');
var sysId = grVo.sc_item_option.value;
return sysId;
}
}
function copySingleAttachment(targetSysId, sourceAttSysId) {
var attRec;
var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', sourceAttSysId);
gr.query();
if (gr.next()) {
var gr1 = new GlideRecord('sys_attachment');
gr1.initialize();
gr1.file_name = gr.file_name;
gr1.content_type = gr.content_type;
gr1.compressed = gr.compressed;
gr1.table_name = 'sc_task';
gr1.size_bytes = gr.size_bytes;
gr1.size_compressed = gr.size_compressed;
gr1.table_sys_id = targetSysId;
attRec = gr1.insert();
}
var attDoc = new GlideRecord('sys_attachment_doc');
attDoc.addQuery('sys_attachment', gr.sys_id);
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();
}
}
var rtim = 'RITM0010265'; // rtim number to copy attachment
var fieldName = 'attachment_3'; // field name of attachment field in rtim
var targetSysId = 'f745ef5e974c111086d3b4b3f153af4a'; // sys_id of record to copy attachment
var fromSysId = getAttachment(rtim, fieldName);
if (!fromSysId) {
gs.error('unable to find field ' + fieldName + ' in rtim ' + rtim);
} else {
copySingleAttachment(targetSysId, fromSysId);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2022 03:46 AM
The attachment field is on the table and its not a variable.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2022 04:02 AM
Then it'll be much easier because the value of the field contains the sys_id of the record in sys_attachment table.
function copySingleAttachment(targetSysId, sourceAttSysId) {
var attRec;
var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', sourceAttSysId);
gr.query();
if (gr.next()) {
var gr1 = new GlideRecord('sys_attachment');
gr1.initialize();
gr1.file_name = gr.file_name;
gr1.content_type = gr.content_type;
gr1.compressed = gr.compressed;
gr1.table_name = 'sc_task';
gr1.size_bytes = gr.size_bytes;
gr1.size_compressed = gr.size_compressed;
gr1.table_sys_id = targetSysId;
attRec = gr1.insert();
}
var attDoc = new GlideRecord('sys_attachment_doc');
attDoc.addQuery('sys_attachment', gr.sys_id);
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();
}
}
var tableName = '<table name to from from>';
var fromSysId = '<sys_id of record>';
var fieldName = '<name of field>';
var targetSysId = 'f745ef5e974c111086d3b4b3f153af4a'; // sys_id of record to copy attachment
var grTable = new GlideRecord(tableName);
grTable.addQuery('sys_id', fromSysId);
grTable.query();
if (grTable.next()) {
var sysId = grTable.getValue(fieldName);
copySingleAttachment(targetSysId, sysId);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2022 09:21 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2022 06:51 AM
Hello,
The "file attachment field" (in the example below named u_current_file) contains the sys_id from the sys_attachment table.
You can use a code like the one below to copy it.
var gr = new GlideRecord('xxxx');
gr.get('f847adac1b40d110be52419fe54bcbca');
var fSys = gr.u_current_file.toString();
gs.info("File sys id: " + fSys);
var gAtach = new GlideRecord('sys_attachment');
gAtach.get(fSys);
gs.info("File Name:" + gAtach.file_name);
var targetRef = new GlideRecord('your_table');
targetRef.get('target_sys_id');
//Example of script
var glideSysAttachmentRef = new GlideSysAttachment();
var guid = "";
var newFileName = gAtach.getValue("file_name");
guid = glideSysAttachmentRef.writeContentStream(targetRef, newFileName, gAtach.getValue("content_type"), glideSysAttachmentRef.getContentStream(gAtach.getUniqueValue()));