Script to delete a record along with related records.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-02-2023 10:15 AM
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
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-02-2023 11:41 AM - edited ‎10-02-2023 11:58 AM
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