0

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){
   aId.add(a.Id);    
 }

 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;
 if(parentObj.startsWith('006')) 

    {

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

                efa.setFileName(alist[0].Name);
                efa.setBody(alist[0].body);

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

                       String[] toAddresses = new String[] {emailId,'[email protected]'};
                       mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
                       mail.setToAddresses(toAddresses);             
                       mail.setSubject('Attachment is deleted');
                       mail.setBccSender(false);
                       mail.setUseSignature(false);
                       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
  • 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
    Commented Sep 29, 2015 at 22:25

1 Answer 1

3

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

10
  • 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
    Commented Sep 29, 2015 at 22:51
  • use userInfo.getUserID()
    – Eric
    Commented 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
    Commented Sep 29, 2015 at 22:55
  • Sorry could you please explain where exactly i have to use that in my trigger?
    – Admin_BA
    Commented 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
    Commented 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 .