+ Start a Discussion
Pallavi Ghole 9Pallavi Ghole 9 

Hitting Error while saving an opportunity ==>System.FinalException: Cannot modify a collection while it is being iterated. Class.OpportunityIUDTriggerHandler.handleBeforeInsert: line 60, column 1 Trigger.OpportunityIUDTrigger: line 3, column 1

Hello Apex Gurus,
I am very new to apex coding so please excuse the lousy code (I know I will have to do a lot of cleanup on this)

I keep hitting a error when I try to save a new opportunity because my trigger calls the code below. 

Any idea what I am doing wrong ? 


Code ==>
========


public class OpportunityIUDTriggerHandler {
    
    public static void handleBeforeInsert(List<Opportunity> newObjs)
    {
        id Go_Plan_id = '012C0000000MYvrIAG';
        
        // Query for all the Account Plans that rollup on Customer Type
        List<Account_Opportunity__c> CTPlans = [SELECT Id, Account_Customer_type_Id__c FROM Account_Opportunity__c
                                                WHERE Rollup_On__c = 'Customer Type'];
                                                
                                                
        // Query for all the Account Plans that rollup on Parent                                        
        List<Account_Opportunity__c> ParentPlans = [SELECT Id, Account__c FROM Account_Opportunity__c
                                                    WHERE Rollup_On__c = 'Parent'];


        // Make a Map that lets you search for Account Plan Id by Customer Type Id
          Map<Id, Account_Opportunity__c> CustTypeToAcctPlanMap = new Map<Id, Account_Opportunity__c>();
          for (Account_Opportunity__c CTAP : CTPlans) 
          {
            CustTypeToAcctPlanMap.put(CTAP.Account_Customer_type_Id__c , CTAP);
          }
        
        
        // Make a Map that lets you search for Account Plan Id by Parent Id
          Map<Id, Account_Opportunity__c> ParentIdToAcctPlanMap = new Map<Id, Account_Opportunity__c>();
          for (Account_Opportunity__c PAP : ParentPlans) 
          {
            ParentIdToAcctPlanMap.put(PAP.Account__c , PAP);
          }
   
    

        //Get unique Account Ids +
        // find all of the new opps with the correct record type, and 
        // pull their *distinct* go plan opportunity IDs.  While we're at it
        // let's limit the opportunities to only those we need to update.
        List<Opportunity> oppList = new List<Opportunity>();
        Set<Id> idSet = new Set<Id>();
        Set<Id> accountIds = new Set<Id>();
                for(Opportunity opp1: newObjs)
                {
                    if (opp1.RecordTypeId == Go_Plan_id ) 
                    {
                        idSet.Add(opp1.Go_Plan_Opportunity__c);
                        oppList.Add(opp1);
                        accountIds.add(opp1.AccountId);
                    }
                }
                   
               
               //Do SOQL Query to get Customer Type Id of Account Id   
                Map<Id, Account> accounts = new Map<Id, Account>(
                    [select id, Customer_Type_Code__c from Account where id in :accountIds]);
              
               id custTypeId = null;
               id planId = null;
               //List<opportunity> oppList = new List<opportunity>{}; 
               
               for(Opportunity opp1: oppList)
               {
                
                   planid = null ;
                   custTypeId = null; 
                   custTypeId = accounts.get(opp1.AccountId).Customer_Type_Code__c;
                   
                   if(custTypeId <> null) 
                   {
                    planid =    CustTypeToAcctPlanMap.get(custTypeId).Id;
                   }
                   
                   else if(planId == null)
                   {
                    planid =    ParentIdToAcctPlanMap.get(opp1.AccountId).Id ;
                   }
                   
                   if (planid <> null)
                   {
                     opp1.Account_Opportunity__c = planid;
                     if(opp1.Sales_Opportunity_Type_KCGGo__c == 'Keep')
                        {
                            opp1.Account_Opportunity_Keep__c = planid;
                            opp1.Account_Opportunity_Convert__c = null;
                            opp1.Account_Opportunity_Grow__c = null;
                            opp1.Account_Opportunity_NPI__c = null;
                            opp1.Account_Opportunity_Pipeline__c= null;
                        }
                     
                     
                     else if(opp1.Sales_Opportunity_Type_KCGGo__c == 'Convert')
                        {
                            opp1.Account_Opportunity_Keep__c = null;
                            opp1.Account_Opportunity_Convert__c = planid;
                            opp1.Account_Opportunity_Grow__c = null;
                            opp1.Account_Opportunity_NPI__c = null;
                            opp1.Account_Opportunity_Pipeline__c= null;
                        }
                        
                    else if(opp1.Sales_Opportunity_Type_KCGGo__c == 'Grow')
                        {
                            opp1.Account_Opportunity_Keep__c = null;
                            opp1.Account_Opportunity_Convert__c = null;
                            opp1.Account_Opportunity_Grow__c = planid;
                            opp1.Account_Opportunity_NPI__c = null;
                            opp1.Account_Opportunity_Pipeline__c= null;
                        }
                        
                        
                    else if(opp1.Sales_Opportunity_Type_KCGGo__c == 'NPI')
                        {
                            opp1.Account_Opportunity_Keep__c = null;
                            opp1.Account_Opportunity_Convert__c = null;
                            opp1.Account_Opportunity_Grow__c = null;
                            opp1.Account_Opportunity_NPI__c = planid;
                            opp1.Account_Opportunity_Pipeline__c= null;
                        }

                    
                    else if(opp1.Sales_Opportunity_Type_KCGGo__c == 'Pipeline')
                        {
                            opp1.Account_Opportunity_Keep__c = null;
                            opp1.Account_Opportunity_Convert__c = null;
                            opp1.Account_Opportunity_Grow__c = null;
                            opp1.Account_Opportunity_NPI__c = null;
                            opp1.Account_Opportunity_Pipeline__c= planid;
                        }
                   
                } 
                  oppList.add(opp1);    
               } 
                  update oppList ;
    
    }
    
    
    public static void handleBeforeUpdate( List<Opportunity> newObjs
                                          , List<Opportunity> oldObjs
                                          , Map<Id, Opportunity> oldMap)
    
    {
    
    }
    
    
    public static void handleBeforeDelete( List<Opportunity> oldObjs)
    {
    
    }
    
    
    public static void handleAfterInsert( List<Opportunity> newObjs)
    {
        
    }
    
    public static void handleAfterUpdate( List<Opportunity> newObjs
                                         , List<Opportunity> oldObjs
                                         , Map<Id, Opportunity> oldMap)
    {
    
    }
    
     public static void handleAfterDelete( List<Opportunity> oldObjs)
    {
        
    }
    
    
}
Best Answer chosen by Pallavi Ghole 9
Pallavi Ghole 9Pallavi Ghole 9
Ok found the issue ==> created another variable for the .add and that seems to have helped. Yay!!! to small victories :)