can anyone help me out in these trigger to update the account checkbox - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Ajay PatelAjay Patel 

can anyone help me out in these trigger to update the account checkbox

Senerio Create 1 checkbox filed on Account with name “IsAccount”  Same on Opportunity with IsOpportunity

now question is if 1 opportunity is creted on account then just simple update the checkbox of isAccount only when opportunity checkbox is true if in opportunity is update to uncheck the issoppportunity then alos upadte to account filed 

now second if 2 or more opportunity is creted then on same account 
then if anyone of opportunity filed isooportunity is true then just update the account filed  "isAccount"to true else not update 
 
My code is below

trigger ThreeOpportunityUpsateAccount on Opportunity (after insert,after update) 
{
     list<Id> accIds = new list<Id>();
     List<Opportunity> oppList = new List<Opportunity>();
     list<Account> accounts = new list<account>();
     for(opportunity o:trigger.new)
     {
     accIds.add(o.accountId);
         
     }
     for(account acc:[select Id, IsAccount__c,(select id,IsOpportunity__c from opportunities) from account where Id IN :accIds])
        {
      if (oppList.size()==1){
           
       
        for(opportunity opp:trigger.new)
        {
            if(opp.IsOpportunity__c==True)
            {
                acc.IsAccount__c=True;
                accounts.add(acc);
            }
            if(opp.IsOpportunity__c==false)
            {
                acc.IsAccount__c=false;
                accounts.add(acc);
            }
         }
      }
                
    if(oppList.size() > 1) 
    {   
        for(opportunity opp:trigger.new)
         if(opp.IsOpportunity__c==True)
            {
                acc.IsAccount__c=True;
                accounts.add(acc);
            }
        
    }
}
     update accounts;
}    


thanks in advance 
Best Answer chosen by Ajay Patel
Lakhan MeghaniLakhan Meghani
trigger OppClosedAccCustomer on Opportunity (before insert, before update ) {
    if(trigger.isbefore) {
        set<id> IdsOfOpportunities = new set<id>();
        for(Opportunity Opp : trigger.new) {  
            IdsOfOpportunities.add(opp.AccountId);
        }
        list<account> acc1 = new  list<account>();
        acc1 = [SELECT Id, Active_Account__c, (select id,Active_Contract__c from 
                                               opportunities where Active_Contract__c = True ) FROM account where Id IN: IdsOfOpportunities];
        list<opportunity> OppList = new  list<opportunity>();
        OppList = [select id,Active_Contract__c from opportunity where Id IN: IdsOfOpportunities];
        
        for(Opportunity Opp : trigger.new) {  
            IdsOfOpportunities.add(Opp.Id );
            for(account a : acc1) {
                if(a.opportunities.size()>1) {
                    for(opportunity o : a.opportunities) {
                        
                       
                            a.Active_Account__c =  True  ;
                        
                        
                    }
                }
                else {
                    a.Active_Account__c =  Opp.Active_Contract__c  ;
                } 
            }
            
        }
        update acc1;
    }
    
}

Try this

Please make a required changes in field API name

Thanks

All Answers

Ines GarciaInes Garcia
Why dont you use a rollup summary, will be either 1 if 1 opp or greater than 1 if more
out of the box 
Lakhan MeghaniLakhan Meghani
trigger OppClosedAccCustomer on Opportunity (before insert, before update ) {
    if(trigger.isbefore) {
        set<id> IdsOfOpportunities = new set<id>();
        for(Opportunity Opp : trigger.new) {  
            IdsOfOpportunities.add(opp.AccountId);
        }
        list<account> acc1 = new  list<account>();
        acc1 = [SELECT Id, Active_Account__c, (select id,Active_Contract__c from 
                                               opportunities where Active_Contract__c = True ) FROM account where Id IN: IdsOfOpportunities];
        list<opportunity> OppList = new  list<opportunity>();
        OppList = [select id,Active_Contract__c from opportunity where Id IN: IdsOfOpportunities];
        
        for(Opportunity Opp : trigger.new) {  
            IdsOfOpportunities.add(Opp.Id );
            for(account a : acc1) {
                if(a.opportunities.size()>1) {
                    for(opportunity o : a.opportunities) {
                        
                       
                            a.Active_Account__c =  True  ;
                        
                        
                    }
                }
                else {
                    a.Active_Account__c =  Opp.Active_Contract__c  ;
                } 
            }
            
        }
        update acc1;
    }
    
}

Try this

Please make a required changes in field API name

Thanks
This was selected as the best answer