Delete CI records through Fix Script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2023 10:58 PM
I've around 750 CI records which were created mistakenly in cmdb_ci_computer table instead of cmdb_ci_computer_peripheral table.
So, I would like to delete those duplicate 750 CI records available in cmdb_ci_computer table through Fix Script only.
How to achieve this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2023 11:10 PM
Just write a regular GlideRecord query and then use deleteMultiple() to get rid of all the results.
var computers = new GlideRecord('cmdb_ci_computer');
computers.addQuery("your encoded query here");
computers.query();
gs.info(computers.getRowCount());
//computers.deleteMultiple();
Just make sure to run it in background first to see how many records your query is returning. If it's the exact amount then you can remove the comment from deleteMultiple. If not, then you have a mistake in your query.
If the peripheral table is extending from cmdb_ci_computer, then you might need to add a condition that says that the class is not the peripheral table, so that you don't accidentally get other records if your query can't be specific.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 01:24 AM - edited 07-21-2023 01:26 AM
This will help you,
var deleteCI = new GlideRecord('cmdb_ci_computer');
deleteCI.addEncodedQuery('pass your encoded query here');
deleteCI.query();
gs.print(deleteCI.getRowCount());
while(deleteCI.next()){
deleteCI.deleteRecord();
deleteCI.setWorkflow(false);
}
Please mark my answer useful if you find this Helpful
Thank you,
Praneeth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2024 02:32 AM
By the way. When using setWorkflow(false) make sure it's BEFORE update(), insert() or deleteRecord().
Basically this script will delete the record, run scripts and then say not to run script. So if for example you had a BR that is run on delete, it will get run if the setWorkflow(false) is set after the deleteRecord method.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 04:13 AM
HI @Bijay Kumar Sha ,
I trust you are doing great.
Please find below code for the same
// Query to identify duplicate records in cmdb_ci_computer table
var grDuplicates = new GlideRecord('cmdb_ci_computer');
grDuplicates.addQuery('your_duplicate_criteria', 'your_value'); // Replace 'your_duplicate_criteria' with the appropriate field and 'your_value' with the specific value to identify duplicates.
grDuplicates.query();
// Loop through the duplicate records and move them to cmdb_ci_computer_peripheral table
while (grDuplicates.next()) {
var grPeripheral = new GlideRecord('cmdb_ci_computer_peripheral');
grPeripheral.initialize(); // Create a new record in the peripheral table
grPeripheral.setValue('field1', grDuplicates.getValue('field1')); // Copy field values from computer to peripheral table
grPeripheral.setValue('field2', grDuplicates.getValue('field2'));
// Copy other relevant fields accordingly
// Insert the record into cmdb_ci_computer_peripheral table
grPeripheral.insert();
// Delete the record from cmdb_ci_computer table
grDuplicates.deleteRecord();
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi