0

I have a requirement where I am checking on account trigger (on account update) if any opportunity is having Closed Lost status then changing account description with count of closed lost opp

I have written some code but its not working for bulk account update and also i am not sure if its correct.can someone please help me .

public class AccountHandler {
    public static boolean flag=true;
    public static void  updateAcc(list<account> accList) {

        set<id>ids=new set<id>();
        for (account acc:accList) {
            ids.add(acc.id);
        }
        System.debug('ids>>>>>>'+ids);
        List<opportunity>opplist=new list<opportunity>([select id,AccountId,StageName from opportunity where accountid in:ids]);
        System.debug('opplist>>>>>>'+opplist);
        map<string,opportunity>oppmap=new map<string,opportunity>();
        for(opportunity opp:opplist){

            oppmap.put(opp.accountid,opp);
        } 
        System.debug('oppmap>>>>>>'+oppmap.keyset()+':::'+oppmap.values());
        list<account>accupdate=new list<account>();
        for(account acc:accList) {
            account accup=new account();
            accup.Id=acc.id;
            integer count=0;
            for(opportunity opp:opplist) {
                if(oppmap.get(acc.id).stagename=='Closed Lost') {
                        System.debug('***Inside If*****');
                        count++;
                        accup.description='There is'+count+' opprotunity with Closed Lost status';

                    }
                    else if(count==0){
                        accup.description='There is No opportunity with this account with Closed Lost deal';
                    }

            }
            }


        System.debug('accupdate>>>>>>'+accupdate);
        update accupdate; 
    }

}
2
  • What do you mean when you say "it's not working for bulk update"? I don't see any of the typical bulkification issues here. Please go into more detail about what "not working" means. – Derek F Sep 10 '19 at 11:55
  • Also, have you tried looking at using a rollup summary field to accomplish this? I think this is possible between Account and Opportunity, and should be able to solve your problem real quick and easy. – Derek F Sep 10 '19 at 11:57
-1

Step

1.I create a Map of accountid to list of opportunity to store the data.

2.I loop through the map and add to list for update the account description

public class AccountHandler {
public static boolean flag=true;
public static void  updateAcc(list<account> accList) {

    set<id>ids=new set<id>();
    for (account acc:accList) {
        ids.add(acc.id);
    }
    Map<Id,List<opportunity>> mapAccountIdOpportunity = new Map<Id,List<opportunity>>();

    for(opportunity opp:[select id,AccountId,StageName from opportunity where accountid in:ids AND stagename=='Closed Lost']){
        if(mapAccountIdOpportunity.containskey(opp.AccountId)
            mapAccountIdOpportunity.get(opp.AccountId).add(opp);
        else
            mapAccountIdOpportunity.put(opp.AccountId, new List<opportunity>{opp}); 
    }

    list<account> accupdate= new list<account>();
    for(Id accId : mapAccountIdOpportunity.keySet()) {
        accupdate.add(new Account(Id = accId,description='There is'+mapAccountIdOpportunity.get(accId).size()+' opprotunity with Closed Lost status'));
    }
    if(!accupdate.isEmpty())
        Update accupdate;
}
1
  • absolutely perfect.thanks – Sandy Sep 10 '19 at 14:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.