- 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-14-2022 11:44 PM
Here is an example, copying attachments from one incident record to another.
var sourceSysID = '9c573169c611228700193229fff72400';
var targetSysID = '9d385017c611228701d22104cc95c371';
var copyAtt = new GlideSysAttachment();
copyAtt.copy('incident',sourceSysID, 'incident',targetSysID);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2022 03:48 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2022 01:31 AM
Hi Jemi,
sys_id of the record in the sys_attachment table is saved in the Options (sc_item_option) table.
To get the sys_id of the variable of type "attachment" in a service catalog, find the corresponding record in Variable Ownerships (sc_item_option_mtom) table. Value of column "Dependent Item" (sc_item_option) is the sys_id of the record in Options (sc_item_option) table. The value of column "Value" (value) is then the sys_id of the record in the sys_attachment table.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2022 02:02 AM
Furthermore, if attachment field is added to a table, the value in the field will be sys_id of the record in the sys_attachment table.
GlideSysAttachment.copy() will copy all the attachment to a record.
To copy just one attachment, check the script in the following thread. sys_attachment table only holds the meta-data of the attachment. The actual file is saved in sys_attachment_doc file so it is necessary to copy records in these tables.
FYI:
Following script will get the sys_id of sys_attachment record related to attachment field in service catalog.
var rtim = 'RITM0010265'; // rtim number to get attachment
var fieldName = 'attachment_3'; // name of attachment field to gety
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; // sysId is the sys_id of record in sys_attachment table
gs.print(sysId);
}