0

I have written trigger on afterdelete. I have checkbox OpenOpportunity on account when i delete the open opportunities stages from account related opportunity then OpenOpportunity checkbox on account should be false. My Logic is here but its not working.

            List<Account>acctsToUpdate=new List<Account>();
            Map<id, Opportunity>oppids=new Map<id, Opportunity>();
            boolean isExit=false;
            if(Trigger.isDelete){
                for(Opportunity opps:(List<opportunity>)trigger.old){
                if(opps.isClosed){
                oppids.put(opps.accountid, opps);
            }
        }
        List<Account>accs=[select id, Opportunity_Open__c,(select accountid,stagename from Opportunities) from account where id in:oppids.keyset())];
        // List<Opportunity> opp=[select id from Opportunity where accountid in:oppids]; 
        for(Account acc:accs){

            for(Opportunity opp:acc.Opportunities){

                if(opp.isClosed==True){

                    acc.Opportunity_Open__c=false;
                    acctsToUpdate.add(acc);
                    isExit=true;
                    break;
                }
            } 
        }
    }

    update acctsToUpdate;
}
  • This won’t even compile as braces are missing. Why not just use a roll up to count open oops the use that to set the checkbox if it is still needed. An aggregat query grouping by account id is another option – Eric Jan 31 '18 at 6:26
  • Hi Eric, There is alredy written trigger on afterupdate and afterinsert for updating checkbox. so i cant change the existing functionality. – user50872 Jan 31 '18 at 6:34
  • Could you please suggest in this code. I am new in sfdc. – user50872 Jan 31 '18 at 6:35
1
trigger oppxTrigger on Opportunity (After delete) {
List<Account> accToUpdate = new List<Account>();
set<Id> accountId = new Set<Id>();
for(Opportunity opp : trigger.old) {
    accountId.add(opp.AccountId);
}

Map<Id,Account> accMap = new Map<Id,Account>([select Id,Opportunity_Open__c from Account where Id In :accountId]);

for(Opportunity opp :trigger.old) {
    if(opp.isClosed!=True){
    Account acc = accMap.get(opp.AccountId);
    acc.Opportunity_Open__c = false;
    accToUpdate.add(acc);
    }
}

   update accToUpdate;

}

try this please

  • Please mark this as best answer so that others will see it. – keepler Jan 31 '18 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy