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

Hello,



Could you please help me restricting duplicate entry while copying "Affected CI' related list from REQ to RITM.


I m using following business rule:



When: before and on update


Table : Request



(function executeRule(current, previous /*null when async*/) {


var reqSys = current.sys_id;


var ritmSys = new GlideRecord("sc_req_item");


ritmSys.addQuery("request", reqSys);   //Compare current request sys_id with request sys_id of RITM


ritmSys.query();


if(ritmSys.next())


{


var reqci = new GlideRecord('task_ci');


reqci.addQuery("task", reqSys);   //Compare current request sys_id with task sys_id of Affected CI


reqci.query();


if (reqci.next()){


reqci.initialise();


reqci.task = ritmSys.sys_id; //


reqci.ci_item = reqci.ci_item ;


reqci.insert(); // Add your code here


}


  }


})(current, previous);




This is copying Affected Ci from REQ to RITM but When we update REQ form multiple time,it create duplicated entry on RITM. How can I restrict that?



Thanks in Advance!!!


Monika


Hi Monika,



I am still a beginner at JavaScript, but from what I can tell you are not comparing against what has been currently copied before.   So having the following line is what helped me to resolve my duplication.   My script is a UI copy / insert, while yours looks like a Business Rule (or Client Script) that is copying the Affected CI's list from the Request to the Request Item.



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



I am not sure you want to do this.   I have struggled with Request > Request Item > Task methodolgy by ServiceNow.   It makes sense, but the triple table integration makes it difficult relate the three together.   While you may still be doing 1 Request > 1 Request Item > 1 Task, in the future you may have 1 Request > 1 or more Request Items in which each Request Item could have 1 to more Tasks.   So in your case, it might be a bad move to copy Request Affected CI's to all the Request Items that are part of the initial Request (parent), because typically each Request Item would have unique Affected CI listing.



Hope this helps.



-Wesley