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
bhagavanmca04@yahoo.combhagavanmca04@yahoo.com 

A trigger on Account can't be deleted. when Account has Contact ot Opportunity.

I hope we are aware of the relationship among the standard Objects Account , Contact and Opportunity.
An Account is the master object of Contacts and Opportunity. So an Account may have or may not have Contact and/or Opportunity.
See, Go to any Account  record, then scroll down to related lists like Contacts and Opportunities. If there is any Contact or Opportunity  for that Account, now if you click on delete button of that Account it should not get deleted.
If there is no Contact or Opportunity for that then it should have to get deleted if you click the delete button.
So Will you Anyone to write a trigger on Account that should have to work as above.

craigmhcraigmh

You can just write a before delete trigger to query for Contacts/Opportunities and use the addError() method if any exist.

MadhuGMadhuG

trigger deltrig on Account (before delete) {
 
 for(account a:trigger.old)
 {
 if([select count() from contact where accountid=:a.id]>2)
 a.adderror('Minimum 1 contact should b there');
}
}