Copy attachment from 1 record to another without using Copy function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2023 08:12 PM
Hi Everyone,
I want to copy attachment from SC_TASK table to SC_REQUEST table. Actually I have made a UI action that attaches the attachment to sc_task table and I want that same attachment to get attached to the request record. I am able to attach the attachment using COPY method in GlideSysAttachment, but there is an issue with this as if someone clicks that UI action more than once, then it again copies all the attachments to target request record. I am using below code that's not working as per expectation:
createAssetConditionReportReq: function(catalogTask) {
try {
var donorAttSysID,
grNewAttachment,
linkToNewRecord,
attDataRecord,
newDocRecord,
grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', "sc_task");
grAttachment.addQuery('table_sys_id', catalogTask.sys_id);
// grAttachment.addQuery('file_name', catalogTask.number + "_" + "Asset Condition.pdf");
grAttachment.addEncodedQuery("sys_created_onONLast minute@javascript:gs.beginningOfLastMinute()@javascript:gs.endOfLastMinute()");
grAttachment.query();
while (grAttachment.next()) {
gs.log("Found ","testing");
donorAttSysID = grAttachment.getValue('sys_id');
gs.log("Found "+donorAttSysID,"testing");
grNewAttachment = copyRecord(grAttachment);
gs.log("Found attach "+grNewAttachment,"testing");
grNewAttachment.setValue('table_name', "sc_request");
grNewAttachment.setValue('table_sys_id', catalogTask.request.sys_id);
grNewAttachment.update();
linkToNewRecord = gs.getProperty('glide.servlet.uri') + grNewAttachment.getLink();
gs.log("Found link "+linkToNewRecord,"testing");
attDataRecord = new GlideRecord('sys_attachment_doc');
attDataRecord.addQuery('sys_attachment', donorAttSysID);
attDataRecord.query();
while (attDataRecord.next()) {
newDocRecord = copyRecord(attDataRecord);
newDocRecord.setValue('sys_attachment', grNewAttachment.getValue('sys_id'));
newDocRecord.update();
}
}
} catch (e) {
gs.log("test error: " + e, "test");
}
},
copyRecord: function(record) {
var i,
recordElement,
recordElementName,
recordTable = record.getTableName(),
recordFields = record.getFields(),
grNewRecord = new GlideRecord(recordTable);
grNewRecord.initialize();
for (i = 0; i < recordFields.size(); i++) {
recordElement = recordFields.get(i);
if (recordElement.getName() != 'sys_id' && recordElement.getName() != 'number') {
recordElementName = recordElement.getName();
grNewRecord.setValue(recordElementName, record.getValue(recordElementName));
}
}
grNewRecord.insert();
return grNewRecord;
}
If anyone knows other method for doing this, please help.
Thanks in Advance

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2023 08:18 PM
Hi Rishabh,
Instead of copying why not create a relation and display it as Related list.
If there is a still a need to copy then you can try adding one more clause to check i.e. with file name
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2023 08:25 PM
Hi @Jaspal Singh,
I tried adding that too but its not copying content of the pdf. It is able to attach empty pdf.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2023 09:20 PM
Hi please check below link it may help you
if you get answer please mark helpful and accept solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2023 05:52 AM
Hi @Harish Bainsla,
Actually by using copy method, we are having a small problem that when we run that API second time then the number of attachment records that are inserted second time are more than actual number of attachments.