I am trying to create a Trigger which will prevent deletion of Account which has atleast one contact linked to it. But its not working. Here is my code
trigger AccountDeleteonPrevention on Account (before delete) {
if(Trigger.isBefore && Trigger.isDelete){
for(Account acc: Trigger.old){
if(acc.Contacts.size() > 0){
acc.addError('You cannot delete this account');
}
}
}
}
When I'm deleting Accounts which have contacts in my org they are getting deleted. Also I see that in debugging the value of account.Contacts.size()
comes to be 0 which is strange. That's the reason my if block is not getting evaluated. I am unable to understand my mistake here. Please help