← Script library

UI Actions

Copy Record Button

Add a form button to create a copy of the current record with all field values.

JavaScript
// Server-side code
(function() {
  // Get current record
  var currentId = current.sys_id.toString();

  // Create new record
  var grNew = new GlideRecord('incident');
  grNew.initialize();

  // Fields to exclude from copy
  var excludeFields = [
    'sys_id',
    'sys_created_by',
    'sys_created_on',
    'sys_updated_by',
    'sys_updated_on',
    'number',
    'opened_at',
    'closed_at',
    'resolved_at',
    'work_start',
    'work_end'
  ];

  // Copy all fields except excluded ones
  var elements = current.getElements();
  for (var i = 0; i < elements.size(); i++) {
    var element = elements.get(i);
    var fieldName = element.getName();

    if (excludeFields.indexOf(fieldName) === -1) {
      grNew.setValue(fieldName, current.getValue(fieldName));
    }
  }

  // Modify short description to indicate copy
  var originalDesc = current.short_description.toString();
  grNew.short_description = '[COPY] ' + originalDesc;

  // Add work note to indicate source
  grNew.work_notes = 'Copied from incident ' + current.number;

  // Insert the new record
  var newId = grNew.insert();

  if (newId) {
    gs.addInfoMessage('Created copy: ' + grNew.number);

    // Redirect to the new record
    action.setRedirectURL(grNew.getLink(true));
  } else {
    gs.addErrorMessage('Failed to create copy');
  }
})();

How to use it

1. Create a new UI Action on your table 2. Set Name to "Copy Record" 3. Set Table to "incident" (or your table) 4. Check "Form button" checkbox 5. Uncheck "Client" checkbox 6. Set Order: 100 7. Paste the code above in the Script field 8. Optionally add a condition: !current.isNewRecord() 9. Test by opening a record and clicking the Copy button

Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.