function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chelsea LukowskiChelsea Lukowski 

Able to choose what attachments get sent from Notes & Attachments

I have create a Visualforce Page and Apex Class to send an email from Opportunities with an attachment from the Notes and Attachments section. It works to send all the Attachments in the Notes and Attachments section, I want to be able to choose which attchments get sent. Any ideas on how I can select the Attachment and only send the ones I want. My Class is below. 

 

Apex Class

 public class email_class{

    public email_class(ApexPages.StandardController controller) {

    }
    Public string ToAddresses {get;set;}
    Public string CCAddresses {get;set;}
    Public string opportunityId {get;set;}
    Public string subject {get;set;}
    public string email_body {get;set;}
    public string emailTo {get;set;}
    public string emailCC {get;set;}
       
    Public PageReference send(){
    
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // set the to address
            mail.setToAddresses(new String[] {emailTo}); 
            mail.setCcAddresses(new String[] {emailCC});   //set the cc address
            mail.setSubject(subject);
            mail.setBccSender(false);
            mail.setUseSignature(false);
            mail.setPlainTextBody(email_body);
            mail.setWhatId(opportunityId);// Set email file attachments 
            
       List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();

            for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :opportunityId]){  // Add to attachment file list  
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();  
                efa.setFileName(a.Name); 
                efa.setBody(a.Body); 
                fileAttachments.add(efa);}
                mail.setFileAttachments(fileAttachments);// Send email
                
                
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
        }
}

 

Abhishek BansalAbhishek Bansal
Hi Chelsea,

On what basis do you want to select Attachments that need to be send.
Is there anything unique in those attachments on the basis of which we can filter the query or Do you want to select the attachment from your VF page.
Please clear the above query so that we can provide you any help.
Please let us know the filter criteria on the attachment which youn want to send.

Thanks,
Abhishek
Chelsea LukowskiChelsea Lukowski
I would like to be able to select the attacments from the VF page before I click send. As of now, I can do that but when I click send it will send all the attchaments and not just the one I selected. The attachments are added by our tadmin account so that would be one way to filter the attachments. Does that help any?
Abhishek BansalAbhishek Bansal
Hi Chelsea,

If you are already selecting the attachments on VF page than there must be a wrapper class included in ypour controller class but i cant see it.
So you have to create a wrapper class in your controller class that will bind the selected attachments from VF page to controller class and after that you will have to change the logic a bit to only send the selected attachment.
Please take help on wrapper class with below link :
https://developer.salesforce.com/page/Wrapper_Class

If you need me to help you with the code than please contact me on Gmail or Skype :
Gmail : abhibansal2790@gmail.com
Skype : abhishek.bansal

Thanks,
Abhishek
Chelsea LukowskiChelsea Lukowski
I attempted to add the wrapper class, but now the email does not send at all and I am unable to save the class witht hte GetSelected Attachment Public list. Any ideas where my road bump is?
public class email_class{
    
    Public string ToAddresses {get;set;}
    Public string CCAddresses {get;set;}
    Public string opportunityId {get;set;}
    Public string subject {get;set;}
    public string email_body {get;set;}
    public string emailTo {get;set;}
    public string emailCC {get;set;}
    
    public class Attachmentwrapper
        {
            public Attachment acc{get; set;}
            public Boolean selected {get; set;}
            public Attachmentwrapper(Attachment a)
            {
                acc = a;
            }
        }
    
               
    public email_class(ApexPages.StandardController controller) {
        opportunityId = ApexPages.currentPage().getParameters().get('id');
    }
    
     List<Attachmentwrapper> AttachmentList = new List<Attachmentwrapper>();
     List<Attachment> selectedAttachments = new List<Attachment>();
       
        public List<Attachmentwrapper> getAttachments()
        {
        for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :opportunityId])
        {      
            AttachmentList.add(new Attachmentwrapper(a));
             }
            return AttachmentList;
        }
    
    Public PageReference send(){
       
    
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // set the to address
        mail.setToAddresses(new String[] {emailTo});
        string [] ccaddress;
            if(emailCC != null && emailCC.trim() != ''){
            ccaddress = emailCC.split(',',0);
            mail.setCcAddresses(ccaddress);
            }
        mail.setSubject(subject);
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setPlainTextBody(email_body);
        mail.setWhatId(opportunityId);// Set email file attachments 

        //selectedAttachments.clear();
        for(Attachmentwrapper accwrapper : AttachmentList){
        if(accwrapper.selected == true){
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :opportunityID]){
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);
       
         selectedAttachments.add(accwrapper.acc);
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
         return null;
         }
}       
       /* public List<Attachment> GetSelectedAttachments()
        {
            if(selectedAttachments.size()>0)
            return selectedAttachments;
            else
            return null;
        }  */  
             
        PageReference pageRef = new PageReference('/' + opportunityId);
        pageRef.setRedirect(true);
        return pageRef;
    }
}