How to copy attachment from one File attachment field to another table

Jemi David1
Tera Expert

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?

1 ACCEPTED SOLUTION

Rafael Krindges
Kilo Guru

Hello,

The "file attachment field" (in the example below named u_current_file) contains the sys_id from the sys_attachment table.

find_real_file.png

 

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()));

 

View solution in original post

10 REPLIES 10

suvro
Mega Sage
Mega Sage

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);

@suvro I want to copy only the attachment that is present in YYY field, not all the attachments from the record.

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

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.

https://community.servicenow.com/community?id=community_question&sys_id=8dc922bbdbfb738023f4a345ca96...

 

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);
}