1

Usual page and custom controller stuff. Nothing fancy. The below code works fine, gives results as expected:

            for (Attachment a : [select id,name,body from attachment where id IN: attach_criteria]){
            messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
            efa.setFileName(a.Name);
            efa.setBody(a.Body);
            fileAttachments.add(efa);
            }
            message.setFileAttachments(fileAttachments);

However, I no longer want to select from attachments. I have all the required attachments stored in a local < list>attachment variable called attach_criteria. How do I make the same transfer from this attach_criteria to fileAttachments so that I can set up the email attachments to be sent just like earlier? I tried using the following for-loop:

            for(i=0; i < attach_criteria.size(); i++){
            messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
            efa.setFileName(attach_criteria[i].Name);
            efa.setBody(attach_criteria[i].Body);
            }

...but I get the error:

SObject row was retrieved via SOQL without querying the requested field: Attachment.Body

Thank you for your time.

UPDATE: Pasting entire code to see the big picture

public class VF11_controller {

            string contact_id{get;set;}
            public EmailTemplate emailTemp{get;set;}
            public string cc{get;set;}
            public string bcc{get;set;}
            list<attachwrapper> attach= new List<attachwrapper>();
            public list<attachment> selectedAttach{
            get{
            if(selectedAttach == null)
            selectedAttach = new list<attachment>();
            return selectedAttach;
            }
            set;
            }
            public attachment attachment1{
            get{
            if(attachment1 == null)
            attachment1 = new attachment();
            return attachment1;
            }
            set;
            }  
            public list<attachment> uploadlist{
            get{
            if(uploadlist == null)
            uploadlist = new list<attachment>();
            return uploadlist;
            }
            set;
            }            
            public list<attachment> attach_criteria{
            get{
            if(attach_criteria == null)
            attach_criteria = new list<attachment>();
            return attach_criteria;
            }
            set;
            }
            public list<attachment> uploadattach{get;set;}
            public string Attach_to_delete_Id{get;set;}
            contact c;
            public List<string> cc_addresses{get;set;}
            public List<string> bcc_addresses{get;set;}
            public VF11_controller(ApexPages.StandardController controller) {
                   emailTemp  = [select Subject,Name,Id,HtmlValue From EmailTemplate where id =: '00X50000001XtXB'];  
                   cc_addresses = new List<string>(); 
                   bcc_addresses = new List<string>();
                   contact_id = apexpages.currentpage().getparameters().get('id');
                   c = [Select FirstName from contact where id=: contact_id];
            }

                public List<attachwrapper> getattach()
                {    
                     attach.clear();
                     if(selectedattach.size() > 0){
                        attach_criteria = [select id,name,bodyLength from attachment where Id NOT IN: selectedattach AND parentId =: '00X50000001XtXB'];
                        if(uploadList.size() > 0){
                           attach_criteria.addall(uploadlist);
                        }
                     }
                     else{ 
                          attach_criteria = [select id,name,bodyLength from attachment where ParentId =: '00X50000001XtXB'];
                          if(uploadList.size() > 0){
                             attach_criteria.addall(uploadList);
                          }
                     }
                     for(attachment a : attach_criteria){          
                     attach.add(new attachwrapper(a));
                     }  
                     return attach;

                }
                public PageReference getSelected()
                {   
                    for(attachwrapper awrapper : attach){
                       if(awrapper.selected == true){                 
                          selectedattach.add(awrapper.acc);
                          for(Integer i=0; i < uploadList.size(); i++){
                              if(uploadList[i].name == awrapper.acc.name){
                                 uploadList.remove(i);
                              }   
                          }
                       }
                    }
                    return null;
                }
                public List<attachwrapper> GetSelectedattachments()
                {
                    if(selectedattach.size()>0){                     
                    return getattach();                    
                    }
                    return getattach();
                }   

                public pagereference sendEmail(){
                    if(cc.length() >0 && cc != null){
                        List<string> split_cc = new List<string>();
                        split_cc = cc.split(';');                        
                        for(string s: split_cc){
                            cc_addresses.add(s);
                        }
                    }
                    if(bcc.length() >0 && bcc != null){
                        List<string> split_bcc = new List<string>();
                        split_bcc = bcc.split(';');                        
                        for(string s: split_bcc){
                            bcc_addresses.add(s);
                        }
                    }
                    messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                    message.setTargetObjectId(contact_id);
                    List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();

                    //for (Attachment a : [select id,name,body from attachment where id IN: attach_criteria])
                    //{
                      //Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                      //efa.setFileName(a.Name);
                      //efa.setBody(a.Body);
                     // fileAttachments.add(efa);
                   // }
                    //for (Attachment a : [select id,name,body from attach_criteria])
                    //{
                      //Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                      //efa.setFileName(a.Name);
                      //efa.setBody(a.Body);
                      //fileAttachments.add(efa);
                    //}
                    for(i=0; i < attach_criteria.size(); i++){
                    Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                    efa.setFileName(attach_criteria[i].Name);
                    efa.setBody(attach_criteria[i].Body);
                    }
                    message.setCcAddresses(cc_addresses);
                    message.setBccAddresses(bcc_addresses);
                    emailTemp.HtmlValue = emailTemp.HtmlValue.replace('{!Contact.FirstName}',c.FirstName);
                    message.setSubject(emailTemp.Subject);
                    message.setHtmlBody(emailTemp.HtmlValue);
                    message.setFileAttachments(fileAttachments);
                    messaging.sendEmail(new Messaging.SingleEmailMessage[]{message});
                    return null;
                }

                public pagereference attach_new(){
                   if(attachment1.name != null){
                        attachment1.OwnerId = UserInfo.getUserId();
                        attachment1.ParentId = '00X50000001XtXB';
                        uploadlist.add(attachment1);
                        attachment1.body = null;
                        attachment1 = new Attachment();
                   }
                        return null;
                }

                public class attachwrapper{
                    public attachment acc{get; set;}
                    public Boolean selected{get; set;}
                    public attachwrapper(attachment a){
                    acc = a;
                    selected = false;
                }
            } 
}
1

You'll still need to query the attachments, because you should not try to store the file contents in your view state (limited to 135KB). For example, if a user uploads a 1.2Mb attachment, your page would fail with an error. So, the method of simply querying the document attachments during the building of your email is the appropriate route; this has an upper limit of 10Mb worth of data instead of just 135Kb that has to be shared between all other objects in memory.

  • Thanks sfdcfox. As far as view state, I nullify the email body so I am able to upload on the VF page attachments as big as 3MB (I just did so; without the null statement, I cannot even upload 140KB). 'attachment' does NOT have ALL the attachments I need. I store user's uploaded attachments (user has option to upload on VF page) in attach_criteria. Attach_criteria also has the required attachments queried from attachment. Therefore, attach_criteria -> FileAttachments. Querying on 'attachment' alone misses out on the uploaded attachments. Please help. Need to get values from attach_criteria OUT. – Rosh Aug 13 '13 at 19:55
  • We're using a similar technique; what we do is to store the attachments on the record (e.g. insert the attachments as they upload them), then query them back when we're ready to send. By nullifying the body, you're actually destroying the contents of the file in memory (e.g. view state), so you have to store them elsewhere (a record you can query later). – sfdcfox Aug 13 '13 at 20:02
  • ah, I tried this but I don't want them reflected on the email template as would be the case when we save the attachments using insert. It's just a one-time thing that the user does - he comes in, uploads on the fly a couple attachments on the VF page that simulates the Send an Email page, and sends these attachments in addition to the ones pulled from the email template. I don't want these one-time uploads attached to the template, which is what would happen if I use insert. attach_criteria has all my attachments neatly stacked up. Need to get them out. – Rosh Aug 13 '13 at 20:08
  • Would you like me to paste more of my code so I would be able to explain better? – Rosh Aug 13 '13 at 20:10
  • 1
    If you'd like. I believe I see where you're going with this; I'd still suggest storing them on the record (not the email template) until you're ready to send. You can delete them after you've sent the email. – sfdcfox Aug 13 '13 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.