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

Want to Delete the Account when Opty Status is closed/won

Hi All,


I have the requirment ,

i have one Account with Name "Test " and it has 10 Opportunities for that Account "Test" I need to allow to delete the Account "Test"  record only if the all Opportunites status have closed/own.

And i am not getting how to develop the trigger .

Can u please help me out who toi build the Logic







Best Answer chosen by Admin (Salesforce Developers) 

try this


trigger checkOpps on Account (before delete) {
List <Id> AccIds = new List <Id>();
List <AggregateResult> ops=[Select AccountId, COUNT(Id) cn From Opportunity Where AccountId in :trigger.old And StageName<>'Closed Won' Group By AccountId];
if(ops!=null && ops.size() >0){
for(Account acc: trigger.old){
for(AggregateResult op:ops){
if(acc.Id == op.get('AccountId') && (Integer)op.get('cn') > 0)
acc.adderror('Open Opportunity exist.');