BR to delete the records from table

roomawakar
Tera Contributor

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);

8 REPLIES 8

Abhishek_Thakur
Mega Sage

Hello @roomawakar ,

You can follow the below script that will help to delete the records.

(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.deleteMultipleRecord();
 }
}

 

})(current, previous);

 

Please mark my answer as accepted solution and give thumbs up, if it helps you.

 

Regards,

Abhishek Thakur

Rajesh Chopade1
Mega Sage

hi @roomawakar 

The issue you're facing is likely due to the fact that you're trying to delete records in a before insert business rule. Business rules with the before trigger run before the record is committed to the database. This means that you cannot delete records in the same context, as the system doesn't allow you to modify or delete records that are part of the operation being executed.

To delete records after they are inserted, you should either use an After Insert Business Rule or handle the cleanup using a Scheduled Script or Background Script.

 

When you run the script in the background, you're directly interacting with the records without any constraints around the timing of the operation. In the case of a business rule with the before trigger, the deletion of the record interferes with the insert process, which is not allowed.

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh

Ashish Parab
Mega Sage

@roomawakar ,

Replace grImpactMaint.deleteRecord(true); by grImpactMaint.deleteRecord();
I have just removed true.

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish



Hi Ashish,

 

It is working partially now after removing 'true' from the method but it is deleting the records where business_service=ci_id as well. I have attached the screenshot below. The highlighted ones, i don't want to delete.

 

roomawakar_0-1733409659792.png