Skip to content

Instantly share code, notes, and snippets.

@thisnameissoclever
Last active December 4, 2024 12:42
Show Gist options
  • Select an option

  • Save thisnameissoclever/cc1d8aa71db117f9624aaa479fb1ea17 to your computer and use it in GitHub Desktop.

Select an option

Save thisnameissoclever/cc1d8aa71db117f9624aaa479fb1ea17 to your computer and use it in GitHub Desktop.
ServiceNow script to copy a specific attachment from one record to another
copySpecificAttachment(donorTable, donorID, recipientTable, recipientID, fileName);
function copySpecificAttachment(donorTable, donorID, recipientTable, recipientID, fileName) {
var donorAttSysID,
grNewAttachment,
linkToNewRecord,
attDataRecord,
newDocRecord,
grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', donorTable);
grAttachment.addQuery('table_sys_id', donorID);
grAttachment.addQuery('file_name', fileName);
grAttachment.query();
while (grAttachment.next()) {
donorAttSysID = grAttachment.getValue('sys_id');
grNewAttachment = copyRecord(grAttachment);
grNewAttachment.setValue('table_name', recipientTable);
grNewAttachment.setValue('table_sys_id', recipientID);
grNewAttachment.update();
linkToNewRecord = gs.getProperty('glide.servlet.uri') + grNewAttachment.getLink();
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();
}
}
//gs.print(linkToNewRecord);
}
function copyRecord(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;
}
@maialithar
Copy link

@thisnameissoclever great script, but there is a problem with it if someone would use it in after insert BR on attachment table (i.e. when copying recently added incident from RITM to SCTASK). it creates an infinite loop of adding attachments because of line 19.
you can quickly fix it by adding another object to copyRecord, like that:
line 19: grNewAttachment = this.copyRecord(grAttachment, {'table_name': recipientTable, 'table_sys_id': recipientID});
line 25: newDocRecord = this.copyRecord(attDataRecord, {});
add between lines 46 and 47: var recordValue = Object.keys(fieldsToOverride).indexOf(recordElementName) > -1 ? fieldsToOverride[recordElementName] : record.getValue(recordElementName);
line 32: copyRecord: function(record, fieldsToOverride) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment