Skip to content

Instantly share code, notes, and snippets.

@kunalkhatri
Created March 20, 2025 06:55
Show Gist options
  • Select an option

  • Save kunalkhatri/e0a0d4e1237070e6140318cbc24d9c37 to your computer and use it in GitHub Desktop.

Select an option

Save kunalkhatri/e0a0d4e1237070e6140318cbc24d9c37 to your computer and use it in GitHub Desktop.
ServiceNow : Copy specific Attachment from one record to another
var CopyAtttachmentUtils = Class.create();
CopyAtttachmentUtils.prototype = {
initialize: function() {
},
copySpecificAttachment : function (donorAttachmentID, recipientTable, recipientID) {
var grNewAttachment;
var attDataRecord;
var newDocRecord;
var grAttachment = new GlideRecord('sys_attachment');
if (grAttachment.get(donorAttachmentID)) {
grNewAttachment = this.copyRecord(grAttachment);
grNewAttachment.setValue('table_name', recipientTable);
grNewAttachment.setValue('table_sys_id', recipientID);
grNewAttachment.update();
attDataRecord = new GlideRecord('sys_attachment_doc');
attDataRecord.addQuery('sys_attachment', donorAttachmentID);
attDataRecord.query();
while (attDataRecord.next()) {
newDocRecord = this.copyRecord(attDataRecord);
newDocRecord.setValue('sys_attachment', grNewAttachment.getValue('sys_id'));
newDocRecord.update();
}
}
},
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;
},
type: 'CopyAtttachmentUtils'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment