Clone Record with Related Items
Deep copy a record including related child records like tasks, attachments, and notes.
Form Button Table: incident
#ui-action #form-button #clone #copy #deep-copy #relationships #server-side
Script Code
JavaScript
1// Server-side code
2(function() {
3 // Validate that we have a record
4 if (current.isNewRecord()) {
5 gs.addErrorMessage('Cannot clone a new record');
6 return;
7 }
8
9 var originalSysId = current.sys_id.toString();
10 var originalNumber = current.number.toString();
11
12 try {
13 // Clone the main record
14 var newIncident = new GlideRecord('incident');
15 newIncident.initialize();
16
17 // Copy all fields except system fields
18 var excludeFields = [
19 'sys_id', 'sys_created_by', 'sys_created_on',
20 'sys_updated_by', 'sys_updated_on', 'sys_mod_count',
21 'number', 'opened_at', 'closed_at', 'resolved_at'
22 ];
23
24 var fields = current.getFields();
25 for (var i = 0; i < fields.size(); i++) {
26 var fieldName = fields.get(i).getName();
27
28 if (excludeFields.indexOf(fieldName) === -1) {
29 newIncident.setValue(fieldName, current.getValue(fieldName));
30 }
31 }
32
33 // Modify cloned record
34 newIncident.short_description = '[CLONE] ' + current.short_description;
35 newIncident.state = '1'; // New
36 newIncident.work_notes = 'Cloned from ' + originalNumber + ' by ' + gs.getUserName();
37
38 // Insert the new incident
39 var newSysId = newIncident.insert();
40
41 if (!newSysId) {
42 gs.addErrorMessage('Failed to clone incident');
43 return;
44 }
45
46 gs.info('Cloned incident ' + originalNumber + ' to ' + newIncident.number);
47
48 // Clone related incident tasks
49 var clonedTasks = 0;
50 var grTask = new GlideRecord('incident_task');
51 grTask.addQuery('incident', originalSysId);
52 grTask.query();
53
54 while (grTask.next()) {
55 var newTask = new GlideRecord('incident_task');
56 newTask.initialize();
57
58 // Copy task fields
59 var taskFields = grTask.getFields();
60 for (var i = 0; i < taskFields.size(); i++) {
61 var taskFieldName = taskFields.get(i).getName();
62
63 if (excludeFields.indexOf(taskFieldName) === -1 && taskFieldName !== 'incident') {
64 newTask.setValue(taskFieldName, grTask.getValue(taskFieldName));
65 }
66 }
67
68 // Link to new incident
69 newTask.incident = newSysId;
70 newTask.short_description = '[CLONE] ' + grTask.short_description;
71 newTask.state = '1'; // New
72
73 newTask.insert();
74 clonedTasks++;
75 }
76
77 // Clone work notes (sys_journal_field)
78 var clonedNotes = 0;
79 var grJournal = new GlideRecord('sys_journal_field');
80 grJournal.addQuery('element_id', originalSysId);
81 grJournal.addQuery('name', 'incident');
82 grJournal.addQuery('element', 'work_notes');
83 grJournal.orderBy('sys_created_on');
84 grJournal.query();
85
86 while (grJournal.next()) {
87 var newJournal = new GlideRecord('sys_journal_field');
88 newJournal.initialize();
89 newJournal.element_id = newSysId;
90 newJournal.name = 'incident';
91 newJournal.element = 'work_notes';
92 newJournal.value = '[CLONED] ' + grJournal.value;
93 newJournal.insert();
94 clonedNotes++;
95 }
96
97 // Clone attachments
98 var clonedAttachments = 0;
99 var grAttachment = new GlideRecord('sys_attachment');
100 grAttachment.addQuery('table_sys_id', originalSysId);
101 grAttachment.addQuery('table_name', 'incident');
102 grAttachment.query();
103
104 while (grAttachment.next()) {
105 // Use GlideSysAttachment to copy
106 var sa = new GlideSysAttachment();
107 sa.copy('incident', originalSysId, 'incident', newSysId);
108 clonedAttachments++;
109 }
110
111 // Optional: Clone related records (customize as needed)
112 // - Problem records
113 // - Knowledge articles
114 // - Related CIs
115 // - Custom relationships
116
117 // Show success message
118 var message = 'Successfully cloned incident to ' + newIncident.number;
119 if (clonedTasks > 0) {
120 message += ' (' + clonedTasks + ' tasks';
121 }
122 if (clonedNotes > 0) {
123 message += ', ' + clonedNotes + ' notes';
124 }
125 if (clonedAttachments > 0) {
126 message += ', ' + clonedAttachments + ' attachments';
127 }
128 if (clonedTasks > 0 || clonedNotes > 0 || clonedAttachments > 0) {
129 message += ')';
130 }
131
132 gs.addInfoMessage(message);
133
134 // Redirect to the new incident
135 action.setRedirectURL('incident.do?sys_id=' + newSysId);
136
137 } catch (e) {
138 gs.addErrorMessage('Error cloning incident: ' + e.message);
139 gs.error('Clone incident error: ' + e.message);
140 }
141
142})();
How to Use
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