Duplicate a Record Along with Related Lists?

Wesley Breshear
Tera Expert

Hello,

Please excuse me, but I am a beginner at JavaScript and I am using other code examples but do not fully understand the code.   I am trying to "copy" an Incident record and include "all" the Affected CI's (related list items) and a Impacted Customer list to a new record (copy).

I am using this URL as my code reference (copy of)" Duplicate a Record Along with Related List - ServiceNow Wiki

It appears to be working for the Affected CI's list. That it is copying the Affected CI list, however, it is making another duplicate of the "New" recored.   So when I copy INC00000100 it makes two INC0000101's. I don't fully understand how "new GlideRecord" works and figure that is were my problem is, but I don't know how to change to code to not make another record but still copy the listed items to the new record.   Can you please help with the code below?  

Script:

//create a new Incident record and populate fields

var newinc = new GlideRecord('incident');

var myUserObject = gs.getUser();

newinc.initialize();

//Main ticket area

newinc.cmdb_ci = current.cmdb_ci;

newinc.description = current.description;

newinc.assignment_group = current.assignment_group;

newinc.assigned_to = myUserObject;

newinc.impact = current.impact;

//many other fields copied, but removed for simplification

//copy list of Affected CI's

var oldid = current.sys_id.toString();

var newid = current.insert();

if (newid) {

  var taskcis = new GlideRecord('task_ci');

  taskcis.addQuery('task',oldid);

  taskcis.query();

  while (taskcis.next()) {

      taskcis.task = newid;

      taskcis.insert();

  }

}

//copy list of Impacted Customers

if (newid) {

  var cComp = new GlideRecord('incident');   // example used:   var cComp = new GlideRecord('u_m2m_companies_change_request');

  cComp.addQuery('u_customers_impacted',oldid);   // example used:   cComp.addQuery('u_change_request',oldid);

  cComp.query();

  while (cComp.next()) {

      cComp.u_customers_impacted = newid;

      cComp.insert();

  }

}


newinc.insert();

//Provide onscreen feedback and set URLs

gs.addInfoMessage('Incident ' + newinc.number + ' created.');

action.setRedirectURL(newinc);

action.setReturnURL(current);

Thank you,

-Wesley

1 ACCEPTED SOLUTION

Wesley Breshear
Tera Expert

Here are the corrections that needed to be made to resovle the various issues I was having.   Green = add/changes, Red = deletions



//create a new Incident record and populate fields


var newinc = new GlideRecord('incident');


var myUserObject = gs.getUserID();


newinc.initialize();



//Main ticket area


newinc.cmdb_ci = current.cmdb_ci;


newinc.description = current.description;


newinc.assignment_group = current.assignment_group;


newinc.assigned_to = myUserObject;


newinc.impact = current.impact;


//many other fields copied, but removed for simplification



//copy list of Affected CI's


var oldid = current.sys_id.toString();


var newid = newinc.insert();



if (newid) {


  var taskcis = new GlideRecord('task_ci');


  taskcis.addQuery('task',oldid);


  taskcis.query();


  while (taskcis.next()) {


      taskcis.task = newid;


      taskcis.insert();


  }


}



newinc.insert();   //removed to stop duplicate ticket creation.



//copy list of Impacted Customers


if (newid) {


  var cComp = new GlideRecord('incident'); // example used: var cComp = new GlideRecord('u_m2m_companies_change_request');


cComp.addQuery('u_customers_impacted',oldid); // example used:   cComp.addQuery('u_change_request',oldid);


  cComp.query();


  while (cComp.next()) {


cComp.u_customers_impacted = newid;


cComp.insert();


  }


}


//Above Section not needed, following line resolved the issue "copy list of Impacted Customers"


                  newinc.u_customers_impacted = current.u_customers_impacted;
                 


//Provide onscreen feedback and set URLs


gs.addInfoMessage('Incident ' + newinc.number + ' created.');


action.setRedirectURL(newinc);


action.setReturnURL(current);


View solution in original post

4 REPLIES 4

sumeet_n
Kilo Guru

Please find below changes to the script. 'current.insert()' in your script is incorrectly used to get the new incident sys_id. That is creating duplicate incident. Make below changes and let me know if it solves the problem or you face any more issues.




//create a new Incident record and populate fields


var newinc = new GlideRecord('incident');


var myUserObject = gs.getUser();


newinc.initialize();



//Main ticket area


newinc.cmdb_ci = current.cmdb_ci;


newinc.description = current.description;


newinc.assignment_group = current.assignment_group;


newinc.assigned_to = myUserObject;


newinc.impact = current.impact;


//many other fields copied, but removed for simplification



newinc.insert();



//copy list of Affected CI's


var oldid = current.sys_id.toString();


var newid = newinc.sys_id;



if (newid) {


  var taskcis = new GlideRecord('task_ci');


  taskcis.addQuery('task',oldid);


  taskcis.query();


  while (taskcis.next()) {


      taskcis.task = newid;


      taskcis.insert();


  }


}



//copy list of Impacted Customers


if (newid) {


  var cComp = new GlideRecord('incident');   // example used:   var cComp = new GlideRecord('u_m2m_companies_change_request');


  cComp.addQuery('u_customers_impacted',oldid);   // example used:   cComp.addQuery('u_change_request',oldid);


  cComp.query();


  while (cComp.next()) {


      cComp.u_customers_impacted = newid;


      cComp.insert();


  }


}






//Provide onscreen feedback and set URLs


gs.addInfoMessage('Incident ' + newinc.number + ' created.');


action.setRedirectURL(newinc);


action.setReturnURL(current);


No Success.   None of the 'copy' related lists are working now, but I am not getting two identical copies.   So partially fixed, but other stuff is broken now.



This is code is not working either.   Supposed capture current User that is copying the Incident record and place their name into the "Assigned to" field.


var myUserObject = gs.getUser();


newinc.assigned_to = myUserObject;



Thank you,


-Wesley


Madhu80
ServiceNow Employee
ServiceNow Employee

Wesley,



There are two inserts in the code that you have posted.



var newid = current.insert();


newinc.insert();



You need to remove either of the above lines depending upon which one you want to insert.


Wesley Breshear
Tera Expert

Here are the corrections that needed to be made to resovle the various issues I was having.   Green = add/changes, Red = deletions



//create a new Incident record and populate fields


var newinc = new GlideRecord('incident');


var myUserObject = gs.getUserID();


newinc.initialize();



//Main ticket area


newinc.cmdb_ci = current.cmdb_ci;


newinc.description = current.description;


newinc.assignment_group = current.assignment_group;


newinc.assigned_to = myUserObject;


newinc.impact = current.impact;


//many other fields copied, but removed for simplification



//copy list of Affected CI's


var oldid = current.sys_id.toString();


var newid = newinc.insert();



if (newid) {


  var taskcis = new GlideRecord('task_ci');


  taskcis.addQuery('task',oldid);


  taskcis.query();


  while (taskcis.next()) {


      taskcis.task = newid;


      taskcis.insert();


  }


}



newinc.insert();   //removed to stop duplicate ticket creation.



//copy list of Impacted Customers


if (newid) {


  var cComp = new GlideRecord('incident'); // example used: var cComp = new GlideRecord('u_m2m_companies_change_request');


cComp.addQuery('u_customers_impacted',oldid); // example used:   cComp.addQuery('u_change_request',oldid);


  cComp.query();


  while (cComp.next()) {


cComp.u_customers_impacted = newid;


cComp.insert();


  }


}


//Above Section not needed, following line resolved the issue "copy list of Impacted Customers"


                  newinc.u_customers_impacted = current.u_customers_impacted;
                 


//Provide onscreen feedback and set URLs


gs.addInfoMessage('Incident ' + newinc.number + ' created.');


action.setRedirectURL(newinc);


action.setReturnURL(current);