2

I was going through documentation about Operations That Don't Invoke Triggers and got confused with Cascading delete operations. Records that did not initiate a delete don't cause trigger evaluation statement.

In our org we use three level cascade delete in various places and never noticed this behavior. Triggers of all the three objects are evaluated every time.

In before trigger of every trigger, I do this:

public static void cleanupChildren(map<Id, Object1__c> mapOld) {
    delete [Select Id FROM Object2__c WHERE Parent__c IN :mapOld.keySet()];
}

I can confirm that all the three triggers are evaluated when the delete operation was initiated by Object1__c. As per the documentation triggers of Object2__c and Object3__ should not be evaluated as they didn't initiate delete operation.

What am I missing here? Did I misinterpret the definition of Cascade deletion?

4

In your use case, you are explicitly calling a Delete DML operation on the child Object2 and that will execute the before/after delete triggers on Object2

But, imagine that the trigger of Object1 did not do an explicit delete call on Object2 children. When Object1 (parent) is deleted by user action (or API/Apex DML), the children are cascaded deleted by SFDC and their before/after delete triggers will not be fired

What the documentation is trying to say is that a behind-the-scenes cascade delete will not automatically trigger before/after delete triggers on the targets of the cascade deletion

Cascade delete is on by default for master-detail relationships. If the master is deleted, the children are implicitly (cascade) deleted and their triggers will not be fired.

  • Thank you very much for your answer. I was actually reading that documentation because you mentioned it in an answer yesterday. Can you please elaborate "behind-the-scenes cascade delete"? Never heard of this. Is this something like deleting detail record when its master is deleted? – Avinash Oct 28 '16 at 17:08
  • @AvinashN Yes, if you delete an account, for example, all contacts are also deleted, and their triggers will not fire. Or if you delete a case, then the EmailMessage triggers won't fire. – sfdcfox Oct 28 '16 at 17:12

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.