I want to send an email with file attached when an attachment is deleted from Opp object. How to modify this trigger to achieve this? iam getting an exception while deleting file

Trigger DeleteAttachment on Attachment (after delete) {
 set<Id> aId = new set<id>();
 for(Attachment a:trigger.old){

 list<Attachment>alist=[select Id, Name,body,ParentId from Attachment  where Id in: aId];
 list<opportunity>crlist =[select Id,OwnerId,owner.name from opportunity where Id =: alist[0].ParentId];
 String parentObj = alist[0].parentId;


      list<User> ulist = [select Id, email from User where Id =:crlist[0].OwnerId];
      String emailId = ulist[0].email;
                Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();


                       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

                       String[] toAddresses = new String[] {emailId,'[email protected]'};
                       mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
                       mail.setSubject('Attachment is deleted');
                       mail.setHtmlBody('Hello'+'&nbsp;'+crlist[0].owner.name+','+'<br></br>'+'&nbsp;'+'&nbsp;'+'&nbsp;'+'&nbsp;'+'A file has been deleted'+'&nbsp;'+'<b>'+crlist[0].Name+'</b>'+'&nbsp;'+'in salesforce,'+'&nbsp;'+'please check the attachment here or to view in salesforce'+'&nbsp;'+'<ahref=https://na27.salesforce.com/'+crlist[0].Id+'>click here</a>'+'<br></br>'+'Thank'+'&nbsp;'+'You');
                       Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});

  • 1
    What error are you getting? Much nicer if you would give the forum a start at where the issue is just in case it is not apparent. is it Index 0 does not exist at String parentObj = alist[0].parentId;
    – Eric
    Sep 29, 2015 at 22:25

1 Answer 1


In an after delete the record is not visible without using ALL ROWS so the query below:

list<Attachment>alist=[select Id, Name,body,ParentId from Attachment  where Id in: aId];

will not return any results and thus

String parentObj = alist[0].parentId;

Will give you index out of bounds

Change to a before delete trigger

  • Thank you, before delete solved the issue. Do you know what needs to be modified so that i can know which user deleted the file? apparently when file is deleted last modified doesn't gets updated so it is difficult to know who deleted it
    – Admin_BA
    Sep 29, 2015 at 22:51
  • use userInfo.getUserID()
    – Eric
    Sep 29, 2015 at 22:54
  • Please mark as accepted answer so others searching for a similar problem can find this question and know it has an answer
    – Eric
    Sep 29, 2015 at 22:55
  • Sorry could you please explain where exactly i have to use that in my trigger?
    – Admin_BA
    Sep 29, 2015 at 23:12
  • @Admin_BA - Where ever it is you want to capture the ID of the user doing the delete
    – Eric
    Sep 29, 2015 at 23:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .