the above trigger will work absolutely fine, but the below code will fail , check this below code

this above code will not give the required error before delete.

Any guess why ?

If you try to execute it will throw an error : System.FinalException: SObject row does not allow errors

as you can see the execution context is not the trigger context in the second piece of code its running over collection of account records although we are using trigger.oldmap to get the record ids, where as in the first code its running over the trigger.old records collection.

Hence we can say “You can you use the addError method for only those records that are available in Trigger Context. “

Related Post

2 thoughts on “Trigger to prevent deletion of account if child cases exist”

Leave a Reply

Your email address will not be published. Required fields are marked *