You need to sign in to do that
Don't have an account?
Chelsea Lukowski
Help with wrapper class to send attachment in email from Opportunity
I am wanting to email selected attachment from the Notes and Attachment Section on an Opportunity. I have created a VF page and Apex Class, but I am having problems with the Wrapper Class. It does not email at all.
Apex Class
VF page
Apex Class
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; } }
VF page
<apex:page standardController="Opportunity" extensions="email_class"> <apex:form > <apex:pageBlock title="Email Details"> <apex:pageBlock title="Reciepient"> <b>TO: </b><br/><apex:inputText value="{!emailTo}"/><p/> <b>CC: </b><br/><apex:inputText value="{!emailCC}"/><br/> <br/> <b>Subject: </b><br/><apex:inputText value="{!subject}" maxlength="200"/><br/> <br/> <b>Body: </b><br/><apex:inputTextArea value="{!email_body}" rows="10" cols="100"/> </apex:pageBlock> <apex:pageBlock title="Attachments"> <apex:pageBlockTable value="{!opportunity.Attachments}" var="wrap"> <apex:column headerValue="Select"> <apex:inputCheckbox value="{!opportunity.IsChecked__c}"/> </apex:column> <apex:column value="{!wrap.createddate}"/> <apex:column value="{!wrap.name}"/> <apex:column value="{!wrap.description}"/> </apex:pageBlockTable><p/> </apex:pageblock> <apex:commandButton value="Send Email" action="{!send}"/> <apex:commandButton value="Canel" action="{!cancel}"/> </apex:pageBlock> </apex:form> </apex:page>
<apex:pageBlock title="Attachments">
<apex:pageBlockTable value="{!Attachments}" var="wrap">
<apex:column headerValue="Select">
<apex:inputCheckbox value="{!opportunity.selected}"/>
</apex:column>
<apex:column value="{!wrap.Name}"/>
</apex:pageBlockTable>
<p/>
</apex:pageblock>
i have remove these two column becauser you are not querying these two colum in SOQL at line number 31 if you want these 2 add the field in that SOQL
<apex:column value="{!wrap.createddate}"/>
<apex:column value="{!wrap.description}"/>
Replace the code at line number 55 to 63 with this code
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
for(Attachmentwrapper accwrapper : AttachmentList)
{
if(accwrapper.selected == true)
{
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(accwrapper .Name);
efa.setBody(accwrapper .Body);
fileAttachments.add(efa);
}
}
I updated my VF and Apex Class with the changes above and now I get 2 errors
1.email_class - Line 81 - expecting a right parentheses, found 'true'
2. email_page - Line 1 - Unknown property 'email_class.Attachmentwrapper.name'
1.email_class - Line 81 - expecting a right parentheses, found 'true'
This error is comeing because there is one extra brakte in the end
2. email_page - Line 1 - Unknown property 'email_class.Attachmentwrapper.name'
this error is comeing because i refer it as accwrapper.Name and it suppose to be like accwrapper.acc.Name
same is fixed in below code