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
LBartonLBarton 

Updating Custom Account Field Not Working when Updating several Opportunities At Once

I have a custom field in Accounts which keeps track of the opportunities it has. when I update one opportunity it works correctly.  When I update severa opportunties at once, then the custom account field does not get updated although the opportunities themselves are correctly updated. the custom field is AC_Marketing_Category__c.  I think it has to do with the account lists, but I am not sure how to change them.
 
try{
    //the trigger will update the account's Accelero Connect Category field
    
    if (! trigger.isDelete) {
    
      List<Id> oppIds = new List<Id>() ;
      List<Id> AccountIds = new List<Id>() ;
      List<Account> AcctToUpdate = new List<Account>() ;
            
            for (opportunity op : trigger.New)
        {
        oppIds.add(op.Id);
         AccountIds.add(op.AccountId);
        } 
    
      Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([Select id, StageName from Opportunity where Accountid in :AccountIDs and name = 'Accelero Connect' and Status__c != 'Inactive']) ;   
          Map<Id,Account> acctMap = new Map<Id,Account>([select id, AC_Marketing_Category__c, name, Closed_Won_Opps__c, Type from Account where id in :AccountIDs]);
                
      for (opportunity op : trigger.New){

        //Find the account for this opportunity which is being updated
        Account oAccount =  acctMap.get(op.AccountId);
        Opportunity ACOpportunity = oppMap.get(op.Id);
    
    string stagelist;
    
          if (oppMap.isEmpty()){  //No AC opportunities
        if (oAccount.Closed_Won_Opps__c == 0 ) {
          oAccount.AC_Marketing_Category__c = 5;
        }
        else {
          oAccount.AC_Marketing_Category__c = 4;
        }
        
          }
          else { //There are ACopportunities so see how many of them are closed/won
            Integer iCountClosedWon = 0;
            
          for(Opportunity acMap: oppMap.values()){       
          if (acMap.StageName == 'Closed Won') {
            iCountClosedWon += 1;
          }           
          }     
            
            if (iCountClosedWon > 0) {
          oAccount.AC_Marketing_Category__c = 1;
            }
            else {
              if (oAccount.Closed_Won_Opps__c == 0){
                oAccount.AC_Marketing_Category__c = 3;
            //update oAccount;   
              }
              else {
                 oAccount.AC_Marketing_Category__c = 2;
              }
              
            }
          }
        
          AcctToUpdate.add(oAccount);
        }
        update AcctToUpdate;
    }




 
BalajiRanganathanBalajiRanganathan
There is no separation of account vs their related opportuities in your logic.

try to use parent child relationship query.

select id, AC_Marketing_Category__c, name, Closed_Won_Opps__c, Type, (Select id, StageName fromOpportunity where name ='Accelero Connect' and Status__c != 'Inactive') from Account where idin :AccountIDs
 
ra811.3921220580267847E12ra811.3921220580267847E12
Hi,


trigger sampleAcctupdate on opportunity(after insert, after update)
{

set<ID> acctIDs= new set<ID>();
for(Opportunity opp:trigger.new)
{

acctIDs.add(opp.accountid);
}



List<Account> updateAccounts= new List<Account>();
for(Account acc2:[select id, name,(select id, name,SampleText__c from Opportunities  ),Text1__c from Account where id IN:acctIDs])
{

List<Opportunity> opp1=acc2.Opportunities;
if(opp1.size()>0)
{

for(Opportunity opp3:opp1)
 {
  if(opp3.SampleText__c=='ddd')
  {
  
  acc2.Text1__c='ZZZZ';
 }
 else
 {
 acc2.Text1__c='AAAA';
 
 }
}
}
updateAccounts.add(acc2);
}

if(updateAccounts.size()>0)
 {
 update updateAccounts;
 }
}
LBartonLBarton
Thanks to both of you for your assistance!  I used your advice and have done the following which seems to work.
 
trigger Update_INT_Marketing_Category_Field on Opportunity (after delete, after insert, after update) {

try{
    //the trigger will update the account's Interface custom field
    
    if (! trigger.isDelete) {
    
			List<Id> AccountIds = new List<Id>() ;
			List<Account> AcctToUpdate = new List<Account>() ;
						
            for (opportunity op : trigger.New)
		    {
			 	AccountIds.add(op.AccountId);
		    } 
		    
			Integer iTotalClosedWon = 0;
			for (Account acc1: [select id, name, (select id, name, StageName from Opportunities where StageName = 'Closed Won') from Account where id IN:AccountIds]){
				
				List<Opportunity>opp2 = acc1.Opportunities;
			
				if (opp2.isEmpty()){
					iTotalClosedWon = 0;
				}
				else {
					iTotalClosedWon = 1;
				}
			}			    

						    
      		for (Account acc2:[select id, name,(select id, name, StageName from Opportunities where name = 'Interface' and Status__c != 'Inactive' ), 
      			INT_Marketing_Category__c, Closed_Won_Opps__c, Type  from Account where id IN:AccountIds])
			{
				
				List<Opportunity> opp1=acc2.Opportunities;
				
        		if(opp1.isEmpty()){  //No INT opportunities
					if (iTotalClosedWon == 0 ) {
						acc2.INT_Marketing_Category__c = 5;
					}
					else {
						acc2.INT_Marketing_Category__c = 4;
					}
				
        		}
        		else { //There are INT opportunities so see how many of them are closed/won
        			Integer iCountClosedWon = 0;
        		
  					for(Opportunity opp3:opp1){       
						if (opp3.StageName == 'Closed Won') {
							iCountClosedWon += 1;
						} 					
  					} 		
        		
        			if (iCountClosedWon > 0) {
						acc2.INT_Marketing_Category__c = 1;
        			}
        			else {
        				if (iTotalClosedWon == 0){
        					acc2.INT_Marketing_Category__c = 3;
						//update oAccount;	 
        				}
        				else {
         					acc2.INT_Marketing_Category__c = 2;
        				}
        			
        			}
        		}
      	
        	AcctToUpdate.add(acc2);
      	}
      	update AcctToUpdate;
  	}
	
	if (trigger.isDelete) {
        
			List<Id> AccountIds = new List<Id>() ;
			List<Account> AcctToUpdate = new List<Account>() ;
						
            for (opportunity op : trigger.Old)
		    {
			 	AccountIds.add(op.AccountId);
		    } 

			Integer iTotalClosedWon = 0;
			for (Account acc1: [select id, name, (select id, name, StageName from Opportunities where StageName = 'Closed Won') from Account where id IN:AccountIds]){
				
				List<Opportunity>opp2 = acc1.Opportunities;
			
				if (opp2.isEmpty()){
					iTotalClosedWon = 0;
				}
				else {
					iTotalClosedWon = 1;
				}
			}			    

						    
      		for (Account acc2:[select id, name,(select id, name, StageName from Opportunities where name = 'Interface' and Status__c != 'Inactive' ), 
      			INT_Marketing_Category__c, Closed_Won_Opps__c, Type  from Account where id IN:AccountIds])
			{
				
				List<Opportunity> opp1=acc2.Opportunities;
				
        		if(opp1.isEmpty()){  //No INT opportunities
					if (iTotalClosedWon == 0 ) {
						acc2.INT_Marketing_Category__c = 5;
					}
					else {
						acc2.INT_Marketing_Category__c = 4;
					}
				
        		}
        		else { //There are INT opportunities so see how many of them are closed/won
        			Integer iCountClosedWon = 0;
        		
  					for(Opportunity opp3:opp1){       
						if (opp3.StageName == 'Closed Won') {
							iCountClosedWon += 1;
						} 					
  					} 		
        		
        			if (iCountClosedWon > 0) {
						acc2.INT_Marketing_Category__c = 1;
        			}
        			else {
        				if (iTotalClosedWon == 0){
        					acc2.INT_Marketing_Category__c = 3;
						//update Account;	 
        				}
        				else {
         					acc2.INT_Marketing_Category__c = 2;
        				}
        			
        			}
        		}
      	
        	AcctToUpdate.add(acc2);
      	}
      	update AcctToUpdate;
  	}
 }
 catch (Exception e ){
    System.debug('Update INT Custom field trigger exception ' + e.getMessage());
      
 }
}