BR to delete the records from table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 06:06 AM
Hi All,
I have requirement to delete records from a table called -'em_impact_maint_ci' where business service is not same as CI. I have written down the before BR on insert but this script isn't deleting the records from the table.
When i ran same piece of code via background script, it is working fine and deleting the records. Can some one please let me know where I am doing the mistake.
(function executeRule(current, previous /*null when async*/ ) {
var grImpactMaint = new GlideRecord('em_impact_maint_ci');
grImpactMaint.query();
while (grImpactMaint.next()) {
if (grImpactMaint.business_service != grImpactMaint.ci_id) {
gs.addInfoMessage(business_service);
grImpactMaint.deleteRecord(true);
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 07:45 AM
Hello @roomawakar,
Please try the below script:
// GlideRecord for 'em_impact_maint_ci' table
var grImpactMaint = new GlideRecord('em_impact_maint_ci');
// Add a condition to retrieve only records where 'business_service' is not equal to 'ci_id'
grImpactMaint.addQuery('business_service', '!=', 'ci_id');
grImpactMaint.query();
while (grImpactMaint.next()) {
grImpactMaint.deleteMultipleRecord();
}
I think Juhi's answer will also work for you. Please let me know if you require anything else.
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 06:43 AM
Hello @roomawakar
For tasks like this, a Scheduled Job is the best approach for performance and maintainability. It ensures that the cleanup operation is isolated and does not interfere with insert or update operations on the em_impact_maint_ci table.
Scheduled job script:
// GlideRecord for 'em_impact_maint_ci' table
var grImpactMaint = new GlideRecord('em_impact_maint_ci');
// Add a condition to retrieve only records where 'business_service' is not equal to 'ci_id'
grImpactMaint.addQuery('business_service', '!=', 'ci_id');
// Query matching records
grImpactMaint.query();
// Loop through the records and delete them
while (grImpactMaint.next()) {
grImpactMaint.deleteRecord();
}
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 06:46 AM
Hello @roomawakar ,
If my response helped with your query. So, could you please accept my solution so that it can help other if they will get into the same kind of scenario.
Regards,
Abhishek
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 01:54 PM
on the 'em_impact_maint_ci' table, the business_service field is a Reference to the 'cmdb_ci_service' table, and the 'ci_id' field is a Reference to the 'cmdb_ci' field. They contain sys_id values for the two records. So those value will never match.
@Abhishek_Thakur There is no GlideRecord method named 'deleteMultipleRecord'. See:
GlideRecordAPI#r_GlideRecord-deleteMultiple?navFilter=deletemultiple
You should test your script before posting.
Same for @Juhi Poddar, the addQuery() method takes a value for the 3rd parameter. test before posting code.