Skip to Content

Copy Record Button

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

Form Button Table: incident
#ui-action #form-button #copy-record #server-side #duplicate

Script Code

JavaScript
1// Server-side code
2(function() {
3  // Get current record
4  var currentId = current.sys_id.toString();
5
6  // Create new record
7  var grNew = new GlideRecord('incident');
8  grNew.initialize();
9
10  // Fields to exclude from copy
11  var excludeFields = [
12    'sys_id',
13    'sys_created_by',
14    'sys_created_on',
15    'sys_updated_by',
16    'sys_updated_on',
17    'number',
18    'opened_at',
19    'closed_at',
20    'resolved_at',
21    'work_start',
22    'work_end'
23  ];
24
25  // Copy all fields except excluded ones
26  var elements = current.getElements();
27  for (var i = 0; i < elements.size(); i++) {
28    var element = elements.get(i);
29    var fieldName = element.getName();
30
31    if (excludeFields.indexOf(fieldName) === -1) {
32      grNew.setValue(fieldName, current.getValue(fieldName));
33    }
34  }
35
36  // Modify short description to indicate copy
37  var originalDesc = current.short_description.toString();
38  grNew.short_description = '[COPY] ' + originalDesc;
39
40  // Add work note to indicate source
41  grNew.work_notes = 'Copied from incident ' + current.number;
42
43  // Insert the new record
44  var newId = grNew.insert();
45
46  if (newId) {
47    gs.addInfoMessage('Created copy: ' + grNew.number);
48
49    // Redirect to the new record
50    action.setRedirectURL(grNew.getLink(true));
51  } else {
52    gs.addErrorMessage('Failed to create copy');
53  }
54})();

How to Use

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

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts