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'+' '+crlist[0].owner.name+','+'<br></br>'+' '+' '+' '+' '+'A file has been deleted'+' '+'<b>'+crlist[0].Name+'</b>'+' '+'in salesforce,'+' '+'please check the attachment here or to view in salesforce'+' '+'<ahref=https://na27.salesforce.com/'+crlist[0].Id+'>click here</a>'+'<br></br>'+'Thank'+' '+'You');
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
}
}
String parentObj = alist[0].parentId;