Last active
December 4, 2024 12:42
-
-
Save thisnameissoclever/cc1d8aa71db117f9624aaa479fb1ea17 to your computer and use it in GitHub Desktop.
ServiceNow script to copy a specific attachment from one record to another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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) {