+ Start a Discussion
shawnnshawnn 

Email alert with unique attachment

To cut to the chase I'm trying to create a trigger that will send an email alert when the stage is changed. Seems easy enough, but the catch is that a different attachment has to be send with each email alert. The attachment would correspond to the account and would be found in Notes & Attachments. I have no prior experience writing apex and was wondering if anyone could help. 


Thanks in advance

 

 

SurekaSureka

Hi,

 

You can make of the below code in your trigger/class:

 

For the attachment:

 

Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('attachment.pdf');
 efa.setBody(b);

 

 For sending mail:

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
    String[] toAddresses = new String[] {'bhuvana.sbh@wipro.com','subramaniyanarc@gmail.com'};     
               mail.setSenderDisplayName('Salesforce Support');
               mail.setBccSender(false);
               mail.setTargetObjectId(Id);
               mail.setTemplateId('............'');
               mail.setsaveAsActivity(false);
               mail.setToAddresses(toAddresses );
              mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); // adding attachment to email
               Messaging.sendEmail(new Messaging.Email[] { mail });

Hope this helps.

 

Thanks


shawnnshawnn

This looks great! I'll try it out and get back to you if anything doesnt work. Thanks a lot!

SMasterSMaster

Hi,

 

I am try to create a class  for send emails with attachment....

 

But i am getting the error: Compile Error: Variable does not exist: Name.. why so....

 

 

public class testout{
 
 public opportunity_proposals__c q { get;set; }
 
    
 public Id oppr     {    get;set;    }      
 public testout(ApexPages.StandardController ctrl)   
 {      
 oppr = ctrl.getRecord().Id;
 }
 
 public Pagereference emailAtt()
 {
 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[]{'SMaster@gmail.com'};
        mail.setToAddresses(toAddresses);
        mail.setTargetObjectId('005Q0000000FR7f');
        mail.setTemplateId('00XQ0000000QULj');
        mail.saveAsActivity = false;
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);

        return null;
}
}