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
BrandiTBrandiT 

Apex Class for Outbound Email with VF page rendeded as PDF - Attachment is blank

I created an apex class for sending outbound emails on a custom objec.t  The class is supposed to referenced a visualforce page rendered as pdf and attach it to the email.

 

I created the visualforce page and the pdf renders correctly when I view it in salesforce.com.  But when I send the email with the pdf attached, the pdf shows up as an attachment, but it is completely blank.

 

I think this has something to do with the apex class not passing the correct id to the VF page, but I'm not sure.  Any idea why this isn't working? 

 

 

public class EmailDraft
{

private Email_Draft__c ed;
public EmailDraft(ApexPages.StandardController controller)
{
this.ed=(Email_Draft__c)controller.getRecord();
}

public PageReference SendEmail()
{

// Reference the attachment page, pass in the account ID  
    
        PageReference pdf =  Page.VPL_Attach;
        pdf.getParameters().put('id',(String)ed.Value_Payload__r.id);
        pdf.setRedirect(true);

        // Take the PDF content 
   
        Blob b = pdf.getContent();

        // Create the email attachment 
   
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName('attachment.pdf');
        efa.setBody(b);


Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {ed.send_to_contact__r.email};
IF(ed.introduction_contact__r.email != null) {
toAddresses.add(ed.introduction_contact__r.email);
}
mail.setToAddresses(toAddresses);
if(ed.send_cc_contact__r.email != null){
String[] ccAddresses = new String[] {ed.send_cc_contact__r.email};
                  mail.setCcAddresses(CcAddresses);   
                  }
mail.setHTMLBody(ed.email_body__c);
mail.setSubject(ed.subject__c);
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


ed.approval_status__c = 'Approved & Sent';
update ed;

     PageReference next = new PageReference('/apex/email_draft_detail?id='+ed.id);
          next.setRedirect(true);
 
     return next;
 }    

}

Ritesh AswaneyRitesh Aswaney

It would seem that you are using this class as a Controller Extension.

 

In which case can you check if you're passing in the Id to the VF Page which invokes this, as the controller.getRecord() will return a record, only if the Id was passed in as a Query Parameter in the initial URL.

 

Also, might be an idea to put some debug statements to check if you're getting a null record all through.

BrandiTBrandiT

I have been trying to do this without success for the last three hours.  Do you have any sample code you can post or can you give me a little more guidance on how to do this?

 

Thanks again!

Ritesh AswaneyRitesh Aswaney

Here's a great example that should sort you out :

 

http://blog.jeffdouglas.com/2010/07/16/create-and-email-a-pdf-with-salesforce-com/

 

good luck ! :)