+ Start a Discussion
MaxFrielMaxFriel 

PDF blank in e-mail attachment...

After an object is created or updated, I want to send out a VF page I made as an attachment.  Here is the trigger I wrote to do this...

 

trigger Medical_Inquiry_After_Upsert on Medical_Inquiry_vod__c (after insert, after update) {

    for(Medical_Inquiry_vod__c mi : Trigger.new){
       MIRFFuture.sendEmail(mi.Name,mi.Id);
    }
}

 And here is the sendEmail function...

global class MIRFFuture{
    
    @future
    public static void sendEmail(String name, String id){
        Messaging.reserveSingleEmailCapacity(1);
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'max.friel@veevasystems.com'};
        mail.setToAddresses(toAddresses);
        mail.setReplyTo('max.friel@veevasystems.com');
        mail.setSenderDisplayName('Max Friel');
        mail.setSubject('Medical Inquiry : ' + name);
        String text = 'Please see attached';
        mail.setPlainTextBody(text);
        mail.setHtmlBody(text);
        Messaging.EmailFileAttachment att = new Messaging.EmailFileAttachment();
        //PageReference pr = new PageReference('/apex/MIRF?id=' + id);
        PageReference pr = Page.MIRF;
        pr.getParameters().put('id',id);
        pr.setRedirect(true);
        att.setBody(pr.getContentAsPDF());
        att.setContentType('application/pdf');
        att.setFileName(name + '.pdf');
        att.setInline(false);
        mail.setFileAttachments(new Messaging.EmailFileAttachment[]{att});
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
    }
    
}

 Everything works pretty much as expect, except when I receive the e-mail the PDF is blank.  

 

Via debug statements I have output the PageReference url, and when I copy that into the browser it brings me to the correct page.  I have also checked the blob size of the getContentAsPDF and it is 794, so there is something there.  Why is my PDF blank?

MaxFrielMaxFriel

So a bit more information.  If I just run the method that sends the e-mail via the developer console it runs the same way.  I created a second method, exact copy with out the @future stamp on it and it worked as expected.  Why would the @future cause the pdf to be blank?  Also, how can I get around this, as to run in a trigger I need it to be a @future method?