Copy 'Related List' items, and remove duplicates.

Wesley Breshear
Tera Expert

Hello,  

I found the code below to copy the 'Affected CI's' (related list) to a new Incident record.   However, I believe there is rule/action that always copies/updates the Affected CI's related list with the current cmdb_ci value of the Incident Record each time it is saved.   So my dilemma is that when I copy an Incident record, I get (2) duplicate 'cmdb_ci' listings within the new record.   Can someone help me with the code, so that during the copy script it compares the current Affected CI list and removes the Affected CI that is equal to the 'current.cmdb_ci' value?   Hope this makes sense.   I am still very week to JavaScript and have no clue how to put in IF condition to check if the Affected CI item is = current.cmdb_ci and don't add to the current copied list.

//Create a new Incident record and populate fields

var newinc = new GlideRecord('incident');

newinc.initialize();

//Main Incident record area with many other fields being copied, but have been removed for simplification

newinc.cmdb_ci = current.cmdb_ci;

//Copy Affected CIs and Create Incident copy

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

  }

}

Here is an example of what is occurring:

[Old Incident]

current.cmdb_ci = Banana

Affected CI's list has the following (3) items:

Apple

Banana

Orange

After copying the Incident record, this is how it looks

[New Incident]

current.cmdb_ci = Banana

Affected CI's list has the following (4) items:

Apple

Banana   //this is the current.cmdb_ci copied by Incident rule/action

Banana   //this one was copied from the old record which was the old cmdb_ci, but should not be repeated for duplicated.

Orange

Thank you,

-Wesley

1 ACCEPTED SOLUTION

Kalaiarasan Pus
Giga Sage

var taskcis = new GlideRecord('task_ci');


  taskcis.addQuery('task',oldid);


taskcis.addQuery('ci_item','!=',current.cmdb_ci);


View solution in original post

6 REPLIES 6

Kalaiarasan Pus
Giga Sage

var taskcis = new GlideRecord('task_ci');


  taskcis.addQuery('task',oldid);


taskcis.addQuery('ci_item','!=',current.cmdb_ci);


Hi Kalaiarasan,



Your change breaks the code and now it no longer copies the Affected CIs (related list) items. taskcis.addQuery('ci_item','!=',current.cmdb_ci); are you sure it is "ci_item"



-Wesley


This is my Copy incident UI action and works just fine.



var newinc = new GlideRecord('incident');


newinc.initialize();


newinc.short_description = current.short_description;


newinc.u_requested_by = current.u_requested_by;


newinc.u_requested_for = current.u_requested_for;


newinc.assignment_group = current.assignment_group;


newinc.assigned_to = current.assigned_to;


newinc.description = current.description;


newinc.u_business_impact = current.u_business_impact;


newinc.category = current.category;


newinc.comments = current.comments.getJournalEntry(-1);


newinc.u_service_affected = current.u_service_affected;


newinc.cmdb_ci = current.cmdb_ci;


newinc.u_service_impact = current.u_service_impact;


newinc.severity = current.severity;


newinc.impact = current.impact;


newinc.priority = current.priority;




var newid = newinc.insert();




if (newid!='') {


  var taskcis = new GlideRecord('task_ci');


  taskcis.addQuery('task',current.sys_id);


  taskcis.addQuery('ci_item','!=',current.cmdb_ci);


  taskcis.query();


  while (taskcis.next()) {


  taskcis.task = newid;


  taskcis.insert();


  }


}




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


gs.addInfoMessage(current.comments.getJournalEntry(-1));


action.setRedirectURL(newinc);


action.setReturnURL(current);


Hi Kalai,



Thanks for sharing your code, I now see what I did wrong.   I had replaced your code line over " taskcis.query();" vs. inserting your line.   Stupid me.


Working great now! Thanks for you help!



Also interesting how my "var oldid = current.sys_id.toString();" works equal to your "taskcis.addQuery('task',current.sys_id);".   Another new thing to learn.


 


//Copy Affected CIs and Create Incident copy


var oldid = current.sys_id.toString();


var newid = newinc.insert();



if (newid) {


  var taskcis = new GlideRecord('task_ci');


    taskcis.addQuery('task',oldid);


  taskcis.addQuery('ci_item','!=',current.cmdb_ci);


    taskcis.query();


  while (taskcis.next()) {


      taskcis.task = newid;


      taskcis.insert();


  }


}



-Wesley