← Script library

UI Actions

Clone Record with Related Items

Deep copy a record including related child records like tasks, attachments, and notes.

JavaScript
// Server-side code
(function() {
  // Validate that we have a record
  if (current.isNewRecord()) {
    gs.addErrorMessage('Cannot clone a new record');
    return;
  }

  var originalSysId = current.sys_id.toString();
  var originalNumber = current.number.toString();

  try {
    // Clone the main record
    var newIncident = new GlideRecord('incident');
    newIncident.initialize();

    // Copy all fields except system fields
    var excludeFields = [
      'sys_id', 'sys_created_by', 'sys_created_on',
      'sys_updated_by', 'sys_updated_on', 'sys_mod_count',
      'number', 'opened_at', 'closed_at', 'resolved_at'
    ];

    var fields = current.getFields();
    for (var i = 0; i < fields.size(); i++) {
      var fieldName = fields.get(i).getName();

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

    // Modify cloned record
    newIncident.short_description = '[CLONE] ' + current.short_description;
    newIncident.state = '1';  // New
    newIncident.work_notes = 'Cloned from ' + originalNumber + ' by ' + gs.getUserName();

    // Insert the new incident
    var newSysId = newIncident.insert();

    if (!newSysId) {
      gs.addErrorMessage('Failed to clone incident');
      return;
    }

    gs.info('Cloned incident ' + originalNumber + ' to ' + newIncident.number);

    // Clone related incident tasks
    var clonedTasks = 0;
    var grTask = new GlideRecord('incident_task');
    grTask.addQuery('incident', originalSysId);
    grTask.query();

    while (grTask.next()) {
      var newTask = new GlideRecord('incident_task');
      newTask.initialize();

      // Copy task fields
      var taskFields = grTask.getFields();
      for (var i = 0; i < taskFields.size(); i++) {
        var taskFieldName = taskFields.get(i).getName();

        if (excludeFields.indexOf(taskFieldName) === -1 && taskFieldName !== 'incident') {
          newTask.setValue(taskFieldName, grTask.getValue(taskFieldName));
        }
      }

      // Link to new incident
      newTask.incident = newSysId;
      newTask.short_description = '[CLONE] ' + grTask.short_description;
      newTask.state = '1';  // New

      newTask.insert();
      clonedTasks++;
    }

    // Clone work notes (sys_journal_field)
    var clonedNotes = 0;
    var grJournal = new GlideRecord('sys_journal_field');
    grJournal.addQuery('element_id', originalSysId);
    grJournal.addQuery('name', 'incident');
    grJournal.addQuery('element', 'work_notes');
    grJournal.orderBy('sys_created_on');
    grJournal.query();

    while (grJournal.next()) {
      var newJournal = new GlideRecord('sys_journal_field');
      newJournal.initialize();
      newJournal.element_id = newSysId;
      newJournal.name = 'incident';
      newJournal.element = 'work_notes';
      newJournal.value = '[CLONED] ' + grJournal.value;
      newJournal.insert();
      clonedNotes++;
    }

    // Clone attachments
    var clonedAttachments = 0;
    var grAttachment = new GlideRecord('sys_attachment');
    grAttachment.addQuery('table_sys_id', originalSysId);
    grAttachment.addQuery('table_name', 'incident');
    grAttachment.query();

    while (grAttachment.next()) {
      // Use GlideSysAttachment to copy
      var sa = new GlideSysAttachment();
      sa.copy('incident', originalSysId, 'incident', newSysId);
      clonedAttachments++;
    }

    // Optional: Clone related records (customize as needed)
    // - Problem records
    // - Knowledge articles
    // - Related CIs
    // - Custom relationships

    // Show success message
    var message = 'Successfully cloned incident to ' + newIncident.number;
    if (clonedTasks > 0) {
      message += ' (' + clonedTasks + ' tasks';
    }
    if (clonedNotes > 0) {
      message += ', ' + clonedNotes + ' notes';
    }
    if (clonedAttachments > 0) {
      message += ', ' + clonedAttachments + ' attachments';
    }
    if (clonedTasks > 0 || clonedNotes > 0 || clonedAttachments > 0) {
      message += ')';
    }

    gs.addInfoMessage(message);

    // Redirect to the new incident
    action.setRedirectURL('incident.do?sys_id=' + newSysId);

  } catch (e) {
    gs.addErrorMessage('Error cloning incident: ' + e.message);
    gs.error('Clone incident error: ' + e.message);
  }

})();

How to use it

1. Create a new UI Action on incident table 2. Set Name to 'Clone with Related Items' 3. Check 'Form button' checkbox 4. Uncheck 'Client' checkbox 5. Set Order: 250 6. Add condition: !current.isNewRecord() 7. Paste the code above 8. Customize which related tables to clone 9. Test thoroughly with various scenarios 10. Document cloning behavior for users

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