Can you clone/copy a change request?

jmbrake
Kilo Expert

Is there a way to take a past change request either approved, cancelled or in waiting approval state and copy it.   From there change the dates and submit it as new?

1 ACCEPTED SOLUTION

Hi Joan,



It is actually really easy. What you need to do is create a new UI Action(you define the type - button, cotext menu, etc.), as:


1. select the table: change_request


2. set the UI Action as Active and select when to show - on insert, update, etc.


3. have the script which creates a new record and carries over all attributes/fields that you wish (please see it attached)


And that should be it.



I hope this would help you. If so, please mark it up.


Good luck in creating!


View solution in original post

33 REPLIES 33

Been looking at this and I have an initial version here, however does not currently do attachments, but I will look to incorporate this.


This routine will also copy any associated Change Tasks


Its main difference is that you initially specify the fields you do not want copying to the new record - For us it is mainly approvals, conflict checking, states and system fields.


the routine will then copy any other fields it finds from the old record to the new record.


the nice thing is that any new fields added to your change request or change task will get copied across automatically without having to visit the UI Action again




// Add all fields you do not want to copy here


var strNoCopy = 'number; state; u_state_text; conflict_status; opened_at; outside_maintenance_schedule; sys_updated_by; sys_updated_on; opened_by; sys_created_by; approval_set; sys_created_on; phase; u_approval_resetl closed_at; approval; sys_mod_count; phase_state; u_total_time_worked; sys_class_namel stage; active; work_notes; comments; parent; change_request';



// set the glide records for the old and new change


var ocrgr = new GlideRecord('change_request');


var ncrgr = new GlideRecord('change_request');




// get the old change details


ocrgr.get(current.sys_id);



// getFields() returns a Java ArrayList


var fields = ocrgr.getFields();



// Enumerate GlideElements in the GlideRecord object that have values


for (var i = 0; i < fields.size(); i++) {


  var glideElement = fields.get(i);


  // see if the element has a value and ensure it is not in the strNoCopy field.   If not, add the value to the field on the


  // new record


  if (glideElement.hasValue() && strNoCopy.toLowerCase().indexOf(glideElement.getName().toLowerCase()) == -1) {


  ncrgr.setValue(glideElement.getName(), glideElement);



  }


}




// Update the record


var sysID = ncrgr.insert();


//current.rfc = sysID;


var mySysID = current.update();



// set the glide records for the old and new change tasks


var octgr = new GlideRecord('change_task');


var nctgr = '';




// query for all all tasks associate with the old change


octgr.addQuery('parent', current.sys_id);


octgr.query();




// for each task found, create a new change task record


while (octgr.next())


{


  // Create new glide record for each new task


  nctgr = new GlideRecord('change_task');



  // Get fields from change task


  var fields = octgr.getFields();



  // Enumerate GlideElements in the GlideRecord object that have values


  for (var i = 0; i < fields.size(); i++) {


  var glideElement = fields.get(i);


  // see if the element has a value and ensure it is not in the strNoCopy field.   If not, add the value to the field on the


  // new record


  if (glideElement.hasValue() && strNoCopy.toLowerCase().indexOf(glideElement.getName().toLowerCase()) == -1) {


  nctgr.setValue(glideElement.getName(), glideElement);



  }


  }




  //Make sure the new change task is assocaited to the new Change record


  nctgr.setValue('parent',ncrgr.sys_id);


  nctgr.setValue('change_request',ncrgr.sys_id);



  // Insert new change task record


  var sysID = nctgr.insert();



}




gs.addInfoMessage("Change " + ncrgr.number + " created");


action.setRedirectURL(ncrgr);


action.setReturnURL(current);


I like what is going on here but have the same problem with this code that was mentioned above.   This code says that it will omit the approval status but never sets it back to "Not Yet Requested."   It appears that when this code is executed that it is creating a duplicate record in an "insert and save" type capacity and then requesting approvals.   I'd like basically this same code but to simply save it without requesting approvals.   What modifications are required to make that happen?


for that client the default status was "Not Yet Requested"


In your case, I would look at adding that as an extra option once all the fields have been copied via the loop


anuguajayreddy
Tera Contributor

Yes Every Org works in a different way in CM, above was our requirement 🙂


Jason Stephens
Kilo Guru

Hi Joan,



It looks like you have a lot of good suggestions already, but one way you could pull this off without any coding would be to create templates.   If you can identify certain situations where you need to "copy" a change and the data is always fairly standard, then create templates for each and just reuse as needed.   Once you apply the template, you can easily change any fields that apply to the current record such as dates.   Just another idea, and one more way you can make the process simpler by using something pre-defined.



Creating a Template - ServiceNow Enterprise Wiki



Jason