Monday, Sep 06, 2010
Login

‘Copy’ UI action for Change requests

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
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();
}
}

7 Comments

  1. This 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.

  2. Hi..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 !

    1. Out 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.

  3. Hi 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

    1. There 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.

      1. Thanks for the info and Help!

        1. No problem. Here’s a link to the article describing the other methods.
          http://www.servicenowguru.com/system-ui/ui-actions-system-ui/copy-ui-action-change-requests-part-2/

 

Recent Comments

  • Scott Stechmesser: Awesome script to use. Works great. How would you modify it to be able to copy a Catalog UI Policy?
  • Tulio: Perfect!!! Thanks for this.
  • Ron Methias: Another reason why I have stopped going to the official SN documentation sites and make the GURU my...
  • Richard Huss: Ingenious – and somewhat simpler than the way the Incident Resolution best practice plugin does...
  • valor: Joe, that’s one of the reasons why I’m parsing out the URL instead of trying to get the page...