Script to delete a record along with related records.

Rajat15
Tera Contributor

I want to write a script that will delete a record along with deleting any record which is related to the one I wanted to delete.

For example. If I delete any incident than its associated ci, ITASK etc. Should be deleted as well

1 REPLY 1

Anand Kumar P
Giga Patron
Giga Patron

Hi @Rajat15 ,

Write after delete business rule on the table incident.

Try it in lower instance when dealing with delete multiple records.

 

 

 

 

(function executeRule(current, previous /*null when async*/) {
    // Check if the current incident has related records that need to be deleted
    var incidentSysId = current.sys_id;
var ciGr = new GlideRecord('cmdb_ci');
    ciGr.addQuery('sys_id', current.cmdb_ci);
    ciGr.deleteMultiple();

    // Delete related TASK records
    var itaskGr = new GlideRecord('task');
    itaskGr.addQuery('number', incidentSysId);
    itaskGr.deleteMultiple();
    
    // Add more related tables and delete queries as needed
    
})(current, previous);

 

 

 

 

Thanks,

Anand