Service Now - Scoped Copy Record Function

Wednesday May 20, 2020

ATF

Script to copy a record.

Code

function copyRecord(record) {
  var doNotCopyFields = ['sys_id', 'number', 'sys_mod_count', 'sys_tags']
	var recordElement;
	var recordElementName;
	var recordTable = record.getTableName();
	var recordFields = record.getElements();
	var newRecord = new GlideRecord(recordTable);
	newRecord.initialize();
	recordFields.forEach( function(recordElement) {
    recordElementName = recordElement.getName();
		if(doNotCopyFields.indexOf(recordElementName) < 0) {
			newRecord.setValue(recordElementName, record.getValue(recordElementName));
		}
  });
	//var newSysId = newRecord.insert(); // Uncomment to insert record...
	return newRecord;
}