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
mgodseymgodsey 

Trigger Error: List index out of bounds: 1

I wrote the following trigger, but when it fires I get an error message saying: Apex trigger TagSalesPlanner caused an unexpected exception, contact your administrator: TagSalesPlanner: execution of AfterUpdate caused by: System.ListException: List index out of bounds: 1: Trigger.TagSalesPlanner: line 22, column 1

 

trigger TagSalesPlanner on Opportunity (after update) {

   for (Opportunity opp : Trigger.new) {
    Opportunity OldOpp = Trigger.oldMap.get(opp.ID);
    if ((opp.ProposalApprovers__c == null || (opp.ProposalApprovers__c != OldOpp.ProposalApprovers__c)) && opp.ApprovalStatus__c == 'Approved'){
        
//get list of Approved Opportunities
    List<Opportunity> ApprovedOpps = [SELECT ID, ProposalApprovers__c
                                      FROM Opportunity
                                      WHERE Id in: trigger.new];
    
//get list of related Approval Processes
    List<ProcessInstance> ap = [SELECT ID, TargetObjectID
                                       FROM ProcessInstance
                                       WHERE TargetObjectID in : ApprovedOpps];   
                                       
    List<ProcessInstanceStep> actor = [SELECT ActorID 
                                       FROM ProcessInstanceStep
                                       WHERE ProcessInstanceID in: ap]; 
                                       
    for (Integer i=0; i<actor.size(); i++){
    ApprovedOpps[i].ProposalApprovers__c += actor[i].ActorId;}
    
    update ApprovedOpps;                                                                  
}  
}
}

 Can someone please help me understand what this error message means and what I can do to fix it? Thank you so much!

 

pujapuja

Hi,

your list "ApprovedOpps" have only one value at their o index and at 

for (Integer i=0; i<actor.size(); i++){
ApprovedOpps[i].ProposalApprovers__c += actor[i].ActorId;

 

u trying to fetch 1 index value of ApprovedOpps ..... 

List index out of bound 1 means u try to take 1 position value from list but the list don't have that index.

 

mgodseymgodsey

Thanks for your help! Just to make sure I understand...is the issue here that no records are actually being added to my "ApprovedOpps" list?