- 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 09:44 PM
Hi,
Created a Script Include to copy attachment field.
var AttachmentUtil = Class.create();
AttachmentUtil.prototype = {
initialize: function() {
},
copyAttachmentField: function(fromTableName, fromRecSysId, fromFieldName, toTableName, toRecSysId, toFieldName) {
var grFromTable = new GlideRecord(fromTableName);
if (!grFromTable.get(fromRecSysId)) {
return 'from table not found.';
}
var fromAttachSysId = grFromTable.getValue(fromFieldName);
var grFromAttach = new GlideRecord('sys_attachment');
if (!grFromAttach.get(fromAttachSysId)) {
return 'from record not found.';
}
var grToTable = new GlideRecord(toTableName);
if (!grToTable.get(toRecSysId)) {
return 'to table not found.';
}
var grSysAttach = new GlideSysAttachment();
var fileName = grFromAttach.getValue("file_name");
var toAttachSysId = grSysAttach.writeContentStream(grToTable, fileName, grFromAttach.getValue("content_type"), grSysAttach.getContentStream(grFromAttach.getUniqueValue()));
var grToAttach = new GlideRecord('sys_attachment');
if (!grToAttach.get(toAttachSysId)) {
return 'to attachment not found in sys_attachment table.';
}
grToAttach.setValue('table_name', 'ZZ_YY' + fileName); // change table name so it will be attached to a field instead of the form
grToAttach.update();
grToTable.setValue(toFieldName, toAttachSysId); // set attachment field
grToTable.update();
return toAttachSysId;
},
type: 'AttachmentUtil'
};
Sample execution script to copy attachment from table AO_Table2s "Attachment1" field to table A0 Table1s" field Attachment1.
var fromTableName = 'u_a0_table2';
var fromRecSysId = 'ef841792970c111086d3b4b3f153af2d';
var fromFieldName = 'u_attachment1';
var toTableName = 'u_a0_table1';
var toRecSysId = 'ea79083c9740111086d3b4b3f153affb';
var toFieldName = 'u_attachment1';
var attachUtil = new AttachmentUtil();
var toAttachSysId = attachUtil.copyAttachmentField(fromTableName, fromRecSysId, fromFieldName, toTableName, toRecSysId, toFieldName);
gs.info('toAttachSysId:' + toAttachSysId);
From table record
To table after execution