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;
}
}
}