0
trigger contaccdelete on Contact (before delete) {
list<id> accounts = new  list<id>();
    for (contact c : trigger.old){
                if(c.AccountId!=null){
            accounts.add(c.AccountId);
        }
}
    if(accounts.size()>0){
       list<account> accs = [select  id from account where id in :accounts];
        delete accs;
    }
}

===================== I got the error

Validation Errors While Saving Record(s) There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger contaccdelete caused an unexpected exception, contact your administrator: contaccdelete: execution of BeforeDelete caused by: System.DmlException: Delete failed. First exception on row 0 with id 0017F00000Aw2dyQAB; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0037F000007zQ1f) is currently in trigger contaccdelete, therefore it cannot recursively delete itself: []: Trigger.contaccdelete: line 10, column 1".

2

When you delete an Account, corresponding contacts are deleted. So in this case, from within Contact delete trigger, you are deleting Accounts, which in turn tries to delete the same contact and hence resulting in that error.

You could move your trigger to after delete, and try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.