Delete Duplicate CI

mike2agk
Tera Contributor

Hello, i have issues  with getting duplicate CI's deleted. i have a script below inserted in a workflow editor so that once i click the task that contains the duplicate Ci and then Remediate button, the scripts runs and deletes the duplicate. I see this works fine with just 2 CI's {of which one is a duplicate}. if i have more than one duplicate CI lets say 7  it just deletes only 1. Looking below, it just deleted the CI associated with  03-23-2018 . I guess its not going beyond the second created  [Order By Descending] while deleting. I have also included the script for your review

03-13-2018

02-23-2018

03-14-2018

01-12-2018

03-23-2018

01-12-2018

03-24.2018

script:

var grResult = new GlideRecord ( 'duplicate_audit_result');
grResult.addQuery ( 'follow_on_task', current.sys_id );
grResult.query();
var cis = [];
while (grResult.next())
cis.push(grResult.getValue('duplicate_ci'));
var grCi = new GlideRecord ( 'cmdb_ci' );
grCi.addQuery ('sys_id', 'IN', cis.toString());
grCi.orderByDesc ('sys_created_on');
grCi.query();
grCi.next();
while (grCi.next())
grCi.deleteRecord();

9 REPLIES 9

Devyani_6
Mega Guru

Hi,

Go through the below links. This might help you.

 

https://community.servicenow.com/community?id=community_question&sys_id=c20a8369db5cdbc01dcaf3231f96192b

https://community.servicenow.com/community?id=community_question&sys_id=27980f21db5cdbc01dcaf3231f961988

https://community.servicenow.com/community?id=community_blog&sys_id=3efc66a5dbd0dbc01dcaf3231f96193b

https://community.servicenow.com/community?id=community_blog&sys_id=f61d62e5dbd0dbc01dcaf3231f96197f

 

Mark helpful, if this helped you.

 

Warm Regards,

Devyani 

find_real_file.png

|www.DxSherpa.com|

Varsha21
Giga Guru

Hi mike

 

firstly it is not recommended delete CI,because it may have relations with other tables,

delete only CI which you are sure they are not having relationship with anywhere,

otherwise its better to change its state like 'obsolete'.

for more details

refer following links

https://communities.bmc.com/thread/144861?start=0&tstart=0

https://community.servicenow.com/community?id=community_blog&sys_id=f61d62e5dbd0dbc01dcaf3231f96197f

https://community.servicenow.com/community?id=community_blog&sys_id=3efc66a5dbd0dbc01dcaf3231f96193b

https://community.servicenow.com/community?id=community_question&sys_id=c20a8369db5cdbc01dcaf3231f96... 

 

whatever script you have provided

 

  • grCi.next();//y you are using this here

while (grCi.next())

  • grCi.deleteRecord(); //instead of this try deleteMultiple() method

but deleteMultiple() method will delete all records

because of that do following steps..

 

* if you are getting all duplicate records,then count records using getRowCount()

* and delete all getRowCount(records) except 1 record using loop.

var rowcount=(something like)gr.getRowCount()

 

eg .  while (grCi.next()<rowcount)

 

 

if helpful mark it correct or helpful.

 

Regards,

Varsha.

 

Hello Varsha,

If I don’t intend to delete duplicate CI but mark them all as ‘obsolete’, as you suggested how would the script be modified so I no longer want this line ‘grCi.deleteRecord();’ but a line or lines that updates the duplicate CI’s to ‘obsolete’

appreciate your feedback

Thanks

Ejike

Hi mike,

 

i would like to know some answers from you.

    do you have duplicates records into the 'cmdb_ci' table?

    if yes How you are populating data into that table?

  so can make proper script for avoid those duplicate records.