How to Develop a script for merging CIs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-19-2023 03:32 AM
How to Develop a script that checks for duplicate CIs based on specific attributes and automatically merges them into a single CI.
can anyone help me out.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-19-2023 03:53 AM
@Anjali97 Finding duplicates is easy but merging will be complicated as how you can decide which one to merge with like both ci 's has different data in few fields without checking we don't know which one is correct right
Here is script to find duplicate CI's. I wrote script based on serial_number as unique.
var arraylist = []; var dup = new GlideAggregate('cmdb_ci'); dup.addAggregate('COUNT','serial_number'); dup.addHaving('COUNT','>', '1'); dup.groupBy('serial_number'); dup.query(); while(dup.next()){ arraylist.push(dup.getValue('serial_number')); } gs.print('Duplicate CIs' + arraylist); for (i=0; i < arraylist.length; i++) { var dupci = new GlideRecord('cmdb_ci'); dupci.addQuery('serial_number', arraylist[i]); dupci.query(); gs.info("CI SN: " + arraylist[i] + " has " + dupci.getRowCount() + " records."); }
Results in:
*** Script: Duplicate CIs,56WHL71,AEX5635Z0001AC,L3BB911 *** Script: CI SN: has 2036 records. *** Script: CI SN: 56WHL71 has 2 records. *** Script: CI SN: AEX5635Z0001AC has 2 records. *** Script: CI SN: L3BB911 has 15 records.
Bharath Chintala
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-19-2023 03:55 AM
Hi @Anjali97 ,
Hope you are doing fine.
1. in order to merge the duplicate CIs , first we need to knoow on what all attributes duplucate records are identified. then we need to write an script to find duplicate Cis using below reference script:
var ci = new GlideRecord('cmdb_ci');
ci.addQuery('duplicate_criteria_field', 'duplicate_value');
ci.query();
var duplicates = [];
while (ci.next()) {
duplicates.push(ci.sys_id);
}
2. Decide which CI will be considered the master CI, i.e., the CI that will retain the merged information.
3. For each set of duplicate CIs, iterate through the duplicates and merge their information into the master CI.
var masterCI = new GlideRecord('cmdb_ci');
masterCI.get(masterCI.sys_id);
for (var i = 0; i < duplicates.length; i++) {
var duplicateCI = new GlideRecord('cmdb_ci');
duplicateCI.get(duplicates[i]);
// Copy relevant fields from duplicate CI to master CI
masterCI.field = duplicateCI.field;
// Update related records to point to the master CI
// ...
// Delete the duplicate CI
duplicateCI.deleteRecord();
}
// Update the master CI to reflect the merged information
masterCI.update();
Regards,
Riya Verma