You need to sign in to do that
Don't have an account?
sravu
Urgent!!!!!!!! Need help with EmailFileAttachment
Hi,
I am trying to send email regarding case from customer portal. I created a visualforce page that will open the email form when the user clicks on send an email link, in that page i am also giving them option to attach email with their emails.
Here is the visualforce page code:
<apex:page standardController="Case" extensions="NewIncidentDetailController"> <apex:form > <apex:outputPanel id="sendEmail"> <apex:outputText value="Additional To" /> <apex:inputTextArea value="{!additionalTo}" /> <apex:inputTextArea value="{!ccAddress}" /> <apex:inputTextarea value="{!bccAddress}" /> <apex:inputText value="{!subject}" /> <apex:inputTextArea value="{!body}" richText="true" /> <apex:commandButton value="Send" action="{!send}"/> <apex:commandButton value="Attach File" action="{!eAttach}" /> <apex:commandButton value="Cancel" action="{!cancel}"/><br/><br/> <apex:outputPanel title="Accompanying File" id="emailAttach"> <b style="font-size:12px;font-family:Verdana;">1.Select the File</b><br/><br/> <font style="font-size:12px;font-family:Verdana;">Click the browse button to find the file</font><br/><br/> <apex:inputFile value="{!emailfilebody}" fileName="{!emailfileName}" ></apex:inputFile><br/><br/> <b style="font-size:12px;font-family:Verdana">2. Click the "Attach File" button.</b><br/><br/> <font style="font-size:12px;font-family:Verdana">Repeat steps 1 and 2 to attach multiple files.</font><br/><br/> <apex:commandButton action="{!emailAttach}" value="Attach" styleClass="attachbutton"/><br/><br/> <b style="font-size:12px;font-family:Verdana">3.Click the Done button to complete the upload process. </b><br/><br/> <font style="font-size:12px;font-family:Verdana">( This will cancel an in-progress upload.)</font><br/><br/> <apex:commandButton value="Done" action="{!done}" styleClass="donebutton"/><br/> </apex:outputPanel> </apex:outputPanel> </div> </td> </tr> </table> </apex:form> </apex:page>
Here is my controller:
public with sharing class NewIncidentDetailController { public Transient String emailfileName {get;set;} public Transient Blob emailfileBody {get;set;} public Transient List<Messaging.Emailfileattachment> efa new List<Messaging.Emailfileattachment>(); public PageReference send(){ try{ Case incident = [Select Id,Contact.Email,Subject,ContactId,OwnerId,Con_Email__c,Owner.Email,Owner.Name,CaseNumber,Thread_id__c from Case where Id=:ApexPages.currentPage().getParameters().get('id')]; String[] toAddress = new String[] {}; String[] CcAddresses = new String[] {}; String[] addBccAddress = new String[] {}; String caseSubject; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); if(additionalto!=NULL && additionalto!=''){ String addTo = additionalto; String[] addToAddresses = addTo.split(';'); addToAddresses.add('sravanthi.esri@gmail.com'); toAddress = addToAddresses; mail.setToAddresses(toAddress); } else{ toAddress = new String[] {'sravanthi.esri@gmail.com'}; mail.setToAddresses(toAddress); } if(ccAddress!=NULL && ccAddress!=''){ String ccAdd = ccAddress; String[] ccAddress = ccAdd.split(';'); CcAddresses = ccAddress; mail.setCcAddresses(CcAddresses); } if(bccAddress!=NULL && bccAddress!=''){ String addBcc = bccAddress; addBccAddress = addBcc.split(';'); mail.setBccAddresses(addBccAddress); } if(Subject!=NULL && Subject!=''){ caseSubject = Subject+' '+incident.Thread_id__c; } else{ caseSubject = incident.Subject+' '+incident.Thread_id__c; } mail.setSubject(caseSubject); mail.setHtmlBody(Body); mail.setWhatId(incident.Id); /*Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); if(!emailAttachments.isEmpty()){ List<document> doc = [Select Id,Name,Body from document where Id in :emailAttachments]; for(document a : doc){ efa.setFileName(a.Name); efa.setBody(a.Body); } mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa} ); }*/ if(!efa.isEmpty()) mail.setFileAttachments(efa); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); PageReference pageRef = new PageReference('/apex/IncidentDetailsPage?id='+ApexPages.CurrentPage().getParameters().get('id')); pageRef.setRedirect(true); return pageRef; }catch(Exception e){ ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage())); return null; } } public PageReference emailAttach(){ if(emailfileName!=NULL && emailfileBody!=NULL){ Messaging.Emailfileattachment emailAttach = new Messaging.Emailfileattachment(); emailAttach.setFileName(emailfileName); emailAttach.setBody(emailfileBody); efa.add(emailAttach); } return null; } }
Can anyone please help me with this isuue.
Thanks in advance
Finally i found a way. I dont know whether it is a best practise or not. But it is working fine now. I am trying to insert all documents as attachments to the case and add the attachment Id to the list and then in the email class I am trying to get those attachments with the help of the list of attachments Id and creating a new Messagin.EmailFileAttachment object as follows. Once the attachment is added to the email i am deleting that attachment.
List<Messaging.EmailFileAttachment> allAttachments = new List<Messaging.EmailFileAttachment>();
if(emailattachments.size()>0){
for(Attachment a : emailattachments){
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setBody(a.Body);
efa.setFileName(a.Name);
allAttachments.add(efa);
delete a;
}
system.debug(allAttachments);
mail.setFileAttachments(allAttachments);
}
public PageReference emailAttach(){
if(emailfileName!=NULL && emailfileBody!=NULL){
try{
Attachment myAttachment = new Attachment();
myAttachment.Body = emailfileBody;
myAttachment.Name = emailfileName;
myAttachment.ParentId = ApexPages.currentPage().getParameters().get('id');
insert myAttachment;
emailattachId.add(myAttachment.Id);
}catch(Exception e){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));
}
}
return null;
}
All Answers
The line where i am getting error is efa.add(emailAttach);
It says: Attempt to de-reference a null object
Hi,
From the above code it looks like your variable "efa" is not getting initialized.
Try to do following:
Hope this will work for you!!
Hi,
Thanks for the reply. I tried to change the code as per your suggestions but still I get the same exception.
Finally i found a way. I dont know whether it is a best practise or not. But it is working fine now. I am trying to insert all documents as attachments to the case and add the attachment Id to the list and then in the email class I am trying to get those attachments with the help of the list of attachments Id and creating a new Messagin.EmailFileAttachment object as follows. Once the attachment is added to the email i am deleting that attachment.
List<Messaging.EmailFileAttachment> allAttachments = new List<Messaging.EmailFileAttachment>();
if(emailattachments.size()>0){
for(Attachment a : emailattachments){
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setBody(a.Body);
efa.setFileName(a.Name);
allAttachments.add(efa);
delete a;
}
system.debug(allAttachments);
mail.setFileAttachments(allAttachments);
}
public PageReference emailAttach(){
if(emailfileName!=NULL && emailfileBody!=NULL){
try{
Attachment myAttachment = new Attachment();
myAttachment.Body = emailfileBody;
myAttachment.Name = emailfileName;
myAttachment.ParentId = ApexPages.currentPage().getParameters().get('id');
insert myAttachment;
emailattachId.add(myAttachment.Id);
}catch(Exception e){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));
}
}
return null;
}