I

often see clients request the ability to copy task records.  There are a variety of ways to implement copying functionality and each method has its trade-offs.  Templates, a checkbox field combined with a business rule, or even activating the ‘Insert’ UI action buttons for the task table are all methods that I’ve seen.

In my experience, the best method is to create a brand new UI action button on the table in question.  Below is a script I created to add a ‘Copy’ button to the ‘Change request’ form. This script should be pretty complete for most implementations.  You may just have to customize a few lines to copy over the appropriate fields.  The main advantage that this script has over other methods is that it also copies all of the ‘Affected CI’ records, Change tasks, and attachments for both the Change request and all Change tasks.

To see some other examples of copy UI action methods click here.
Copy Change UI action
Name: Copy change
Form button: True
Table: Change Request
Condition: gs.hasRole(“itil”) && current.isValidRecord()
Script:

var chgID = current.sys_id.toString();
var newChange = new GlideRecord('change_request');
newChange.initialize();
newChange.short_description = current.short_description;
newChange.requested_by = current.requested_by;
newChange.category = current.category;
newChange.type = current.type;
newChange.assignment_group = current.assignment_group;
newChange.assigned_to = current.assigned_to;
newChange.description = current.description;
newChange.risk = current.risk;
newChange.urgency = current.urgency;
newChange.implementation_plan = current.implementation_plan;
newChange.test_plan = current.test_plan;
newChange.backout_plan = current.backout_plan;
newChange.insert();

//Copy attachments for this change
if (typeof GlideSysAttachment != 'undefined')
   GlideSysAttachment.copy('change_request', chgID, 'change_request', newChange.sys_id);
else
   Packages.com.glide.ui.SysAttachment.copy('change_request', chgID, 'change_request', newChange.sys_id);
copyTask();
copyCI();
gs.addInfoMessage('Change ticket ' + newChange.number + ' created.')
action.setRedirectURL(newChange);

function copyTask() {
//Find the current change tasks and copy them
var tasks = new GlideRecord('change_task');
tasks.addQuery('change_request', current.sys_id);
tasks.query();
while(tasks.next()){
var taskID = tasks.sys_id.toString();
//Copy the task record to a new task record
var newTask = new GlideRecord('change_task');
newTask.initialize();
newTask.change_request = newChange;
newTask.order = tasks.order;
newTask.short_description = tasks.short_description;
newTask.description = tasks.description;
newTask.assignment_group = tasks.assignment_group;
newTask.assigned_to = tasks.assigned_to;
newTask.insert();

//Copy attachments for this task
if (typeof GlideSysAttachment != 'undefined')
   GlideSysAttachment.copy('change_task', taskID, 'change_task', tasks.sys_id);
else
   Packages.com.glide.ui.SysAttachment.copy('change_task', taskID, 'change_task', tasks.sys_id);
}
}

function copyCI() {
//Copy over the affected CI list
var currentCI = new GlideRecord('task_ci');
currentCI.addQuery('task', current.sys_id);
currentCI.addNullQuery('u_ci_group'); //Added to ensure that copying does not duplicate Group CIs
currentCI.query();
while(currentCI.next()){
var newCI = new GlideRecord('task_ci');
newCI.initialize();
newCI.task = newChange.sys_id;
newCI.ci_item = currentCI.ci_item;
newCI.insert();
}
}