function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ExploreForceExploreForce 

Trigger to delete the registration if checkbox is checked

I have written a trigger to automate deletion process if user checks the checkbox.

trigger checkCheckBox on Registration__c(after update) {
    //List to contain Registration(s) to be deleted.
    Registration__c[] toBeDeleted = new list<Registration__c>();

    for (Registration__c Register : Trigger.new) {
       if (Register.Registration_cancel__c== True) {
          toBeDeleted.add(Register);
       }
    }
    delete toBeDeleted;
}

The above code sample is throwing following exception:

caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old
Trigger.deleteDeletedOrgs: line 10, column 1.

How to resolve this?
Best Answer chosen by ExploreForce
kiranmutturukiranmutturu
change like this

toBeDeleted.add(new Registration__c(id = Register.id);