1

I All i seem to get stuck with my trigger. I'm very new to triggers i get this error on deployment:

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

Could you please explain why this is happening? Below is 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.size()>0 && 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.');    
       }    
      }
    }
  • 1
    You should remove the SOQL from for loop – RCS Jan 20 '17 at 10:08
3

This is the line Due to this you are getting this error

if (links.size()>0 && Approval.isLocked(links.get(1).LinkedEntityId))

You need to change this

if (links.size()>0 && Approval.isLocked(links.get(0).LinkedEntityId))

or

if (links.size()>1 && Approval.isLocked(links.get(1).LinkedEntityId))

Because if size is 1 and you are trying to access second element (remember index starts from 0) so you need to modify condition as given above.

Code sample

trigger NoteOnContentDocument on ContentDocument (before delete) {
        for (ContentDocumentLink c : [ SELECT LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN: Trigger.old]){          
             if(Approval.isLocked(c.LinkedEntityId)){
          c.addError('Approval pending. You do not have the permission to edit/delete this note, please contact your administrator.');    
       }    
      }
    }

In this way you can remove query from for loop.

| improve this answer | |
  • @Gianni check my updated answer. – Tushar Sharma Jan 20 '17 at 10:56
  • @Gianni check my updated answer. – Tushar Sharma Jan 20 '17 at 13:28
  • Thanks i tries it but the error states: Loop variable must be of type ContentDocument Help?? – Eager 2 Learn apex Jan 20 '17 at 13:32
  • @Gianni I jus found that second option will not work. So you need query, – Tushar Sharma Jan 20 '17 at 13:37
  • @Gianni please post this as separate question and if this solves your problem then you can accept this answer. – Tushar Sharma Jan 20 '17 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.