Name

global.insertRecord

Description

Insert record

Script

function insertRecord(current){
if (typeof current.number != 'undefined' && current.number)
    current.number = ""; // generate a new number
  else if (typeof current.u_number != 'undefined' && current.u_number)
    current.u_number = ""; // generate a new number

  var fieldList = [];
  var fileAttachmentOldIds = {};
  var oldId = current.getValue('sys_id');
  var table = current.getTableName();
  var td = GlideTableDescriptor.get(table);
  var elements = td.getSchemaList();
  for (var i = 0, len = elements.size(); i < len; i++) {
    var ed = elements.get(i);
    var fieldType = ed.getInternalType();
    if (fieldType == "user_image" || fieldType == "file_attachment") {
      var fieldName = ed.getName();
  	if (fieldType == "user_image") {
  		fieldList.push(fieldName);
  	} else {
  		fieldList.push(current.getDisplayValue(ed));
  		fileAttachmentOldIds[current.getValue(ed)] = fieldName;
  	}
      current[fieldName] = undefined; // remove the existing
    }
  }
  var newId = current.insert();
  if (newId) {
      var attachmentMap = GlideSysAttachment.copyAttachmentsByFieldNames(table, oldId, table, newId, fieldList);
  	if(attachmentMap != null && attachmentMap.size() > 0) {
  		for (var key = 0; key < fieldList.length - 1; key++) {
  			if (attachmentMap.get(key)) {
  				var oldCommaNewIds = attachmentMap.get(key);
  				var oldAndNew = oldCommaNewIds.split(",");
  				if (fileAttachmentOldIds[oldAndNew[0]])
  					current[fileAttachmentOldIds[oldAndNew[0]]] = oldAndNew[1];
  			}
  		}
  		current.update();
  	}
      return newId;
  }
  return oldId;
}

Sys ID

4401d68e5303330063ddddeeff7b1284

Offical Documentation

Official Docs: