+ Start a Discussion
Apex developer 21Apex developer 21 

List index out of bounds: 1 Trigger.NoteOnContentDocument: line 9, column 1: []

I have this following class that i want to deploy but i keep getting this error: List index out of bounds: 1 Trigger.NoteOnContentDocument: line 9, column 1: [] 

Could anyno help please with my code:
trigger NoteOnContentDocument on ContentDocument (before delete) {
    for (ContentDocument c : Trigger.old){          
        List<ContentDocumentLink> links = [
            SELECT LinkedEntityId 
            FROM ContentDocumentLink 
            WHERE ContentDocumentId= :c.Id
        ];
     if(!links.isEmpty()){
        if (Approval.isLocked(links.get(1).LinkedEntityId)){
      c.addError('Approval pending. You do not have the permission to edit/delete this note, please contact your administrator.');
	}  
   }	
  }
}

 
Best Answer chosen by Apex developer 21
bob_buzzardbob_buzzard
Apex arrays are index origin zero, so the first element in the array needs to be accessed using the index 0:
 
if (Approval.isLocked(links.get(0).LinkedEntityId)){

 

All Answers

bob_buzzardbob_buzzard
Apex arrays are index origin zero, so the first element in the array needs to be accessed using the index 0:
 
if (Approval.isLocked(links.get(0).LinkedEntityId)){

 
This was selected as the best answer
Banwari kevat1Banwari kevat1
error: List index out of bounds: 1 is shows that list has elements with index less than one or in other way Index of array is start from 0 in apex therefore fist element will be on index 0.Here the root cause of error is links.get(1).So use links.get(0). It will work fine.