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.
Name: Copy change
Form button: True
Table: Change Request
Condition: gs.hasRole(“itil”) && current.isValidRecord()
Script:
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
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
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.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();
}
}
Comments
Posted On
May 10, 2010Posted By
Sabrina EthridgeThis is great… Thanks. Is there something that I could add to this UI Action that would bypass any missing required fields during the copy process? This code appears to try to save the current change prior to making the copy.
Posted On
Jun 20, 2010Posted By
AshutoshHi..this is awesome, Thanxx a ton..! but unfortunate this is not my current requirement!
My requirement is to relate multiple problem tickets with one Change request record..however currently one PRBXXX is related with one CHGXXX record.
I have found UI action, and business rule where changes need to make, but not able to get different logics..
Any ideas to get this done !
Posted On
Jun 21, 2010Posted By
Mark StangerOut of box, a problem can be related to a single change request, but a change request can be related to many problem tickets. Any time you have a reference field on a form, it represents a one-to-many relationship with the table it references. With problem-change, you have a reference field on the problem form, and a related list on the change form. You need to personalize the change request related lists and add the problems list.
Posted On
Jul 23, 2010Posted By
Scott HetzelHi Mark,
Is there a way to do this exact action but not automatically insert the record. Require the user to submit after filling out some Change Request specific fields on the new record which is displayed after pressing the UI Action?
Thanks – Scott
Posted On
Jul 23, 2010Posted By
Mark StangerThere isn’t with this method. There is another way of doing the same thing that would allow you to select a change request to copy from (from a reference field). If you were to use this method, then you could put that field on a change request form with the few fields you needed to override, require those fields to be filled in, and then copy those values over the newly-created change.
I’ve been meaning to post that method for a while. I’ll try to post it next week.
Posted On
Jul 26, 2010Posted By
Scott HetzelThanks for the info and Help!
Posted On
Jul 27, 2010Posted By
Mark StangerNo problem. Here’s a link to the article describing the other methods.
http://www.servicenowguru.com/system-ui/ui-action…
Posted On
Aug 16, 2011Posted By
Scott HetzelMark – do you know how I could change your code to NOT insert the record until the required fields are filled out by user? (similar to how the Global ‘New’ UI Action works)
My problem is people will copy a Change with my UI Action, then close out their browser or navigate to another area of ServiceNow and not fill in my required fields..
Thx..
Posted On
Aug 16, 2011Posted By
Mark Stanger@scott – You can do a change copy without an insert, but you have to pass all of the information over the URL in a ‘sysparm_query’ parameter and then redirect the user to the URL in order to make it work. With a full record copy, that’s usually way too much information to handle that way. If you want to give it a try, you can construct your URL as shown here…
http://www.servicenowguru.com/service-now-general-knowledge/populating-default-values-url-module/
Posted On
Aug 16, 2011Posted By
Scott HetzelI’m going to recommend we enforce policy/procedures (let the wrist slapping begin!) Thanks Mark for the info..