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
Ramana123Ramana123 

How to solve the array index out of bound exception .


The Highlighted lines Getting array index out of bound exception
and hoe to update map last line 


public class CampaignAccountHelper {
    public void updateAndInsertOpportunity(List<CampaignAccount__c> campaignList)
    {
        List<CampaignAccount__c> records = new List<CampaignAccount__c>();    
        Map<Id,List<Opportunity>> mapList = new Map<Id,List<Opportunity>>();
        List<Opportunity> newOpp = new List<Opportunity>();
        For(Account acc :[SELECT id,(SELECT id,Name,CampaignCount__c FROM Opportunities)FROM Account]){
            mapList.put(acc.Id,acc.Opportunities);
        }        
        System.debug('aaaaaaaaaaaaaa'+mapList);
        for(CampaignAccount__c camp :campaignList){
            if(camp.Account__c != NULL){
                records.add(camp); 
            }
        }     
        Integer i = 0;
        System.debug('bbbbbbbbbbbb'+records); 
        if(mapList.size() > 0)
        {
        for(CampaignAccount__c acc : records){  
         
            if(mapList.containskey(acc.Account__c) && acc.Name == mapList.get(acc.Account__c)[i].Name)
            {
              mapList.get(acc.Account__c)[i].CampaignCount__c = mapList.get(acc.Account__c)[i].CampaignCount__c + 1 ; 
            }
           i = i+1; 
        }
        }
       update mapList.values();
    }
}

 
ravi soniravi soni
hi Ramana,
replace your main class with my class.
public class CampaignAccountHelper {
    public void updateAndInsertOpportunity(List<CampaignAccount__c> campaignList)
    {
        List<CampaignAccount__c> lstCampAcc = new List<CampaignAccount__c>();    
        Map<Id,List<Opportunity>> mapOppRec = new Map<Id,List<Opportunity>>();
        List<opportunity> lstOpp = new List<opportunity>();
        
        List<Opportunity> newOpp = new List<Opportunity>();
        For(Account acc :[SELECT id,(SELECT id,Name,CampaignCount__c FROM Opportunities) FROM Account]){
            mapOppRec.put(acc.Id,acc.Opportunities);
        }        
        for(CampaignAccount__c camp :campaignList){
            if(camp.Account__c != NULL){
                lstCampAcc.add(camp); 
            }
        }     
        
        if(mapOppRec.size() > 0){
        for(CampaignAccount__c camAcc : lstCampAcc){  
            for(Opportunity opp : mapOppRec.get(camAcc.Account__c)){
                if(mapOppRec.containskey(camAcc.Account__c) && camAcc.Name == opp.Name){
              opp.CampaignCount__c = opp.CampaignCount__c != null ? opp.CampaignCount__c+1 : '0'; 
            lstOpp.add(opp);
                }   
            }
          }
        } 
       system.debug('lstOpp============> ' + lstOpp);
        if(lstOpp.size() > 0){
        update lstOpp;     
        }
        
       
    }
}

let me know if it helps you and mark it as best.
Thank you