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
Noor FazliNoor Fazli 

Getting blank attachment while sending Email with Generated PDF as attachment from Trigger

I have followed Jitendra's following link to attach visual force page as attachment to the sent email via trigger. But there is one exception my visual force page is linked to standard controller and needs id as parameter to display data.
So far I'm able to get the email with blank attachment.
 
PageReference ref = Page.Detail;
ref.getParameters().put('id',id);

http://www.jitendrazaa.com/blog/salesforce/apex/send-email-with-generated-pdf-as-attachment-from-trigger/

Is there anything else that needs to be done to pull the data into attachment?
 
viruSviruS
Try this 

  PageReference pdf = Page.ReceiptPdfGeneratorTemplate;
        pdf.getParameters().put('id',currentPayment.id);
        pdf.setRedirect(true);
        Blob pdfContent;
        try {
            pdfContent = pdf.getContent();
           // Define the email 
   
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            
            list<String> selectedContactMails = new list<String>();
            for(wrapContact wc:wrapContactList){
                if(wc.selected){
                   selectedContactMails.add(wc.con.Email);
                }
            }
            String[] toAddresses = selectedContactMails;
           
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName('attachment.pdf');
            efa.setContentType('application/pdf');
            efa.setBody(pdfContent);
           
            email.setSubject('Payment Receipt');
           
            email.setToAddresses(toAddresses);
            email.setPlainTextBody('Hi, Please find attached payment receipt');
            email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
            // Sends the email 
       
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  
            //System.debug('>>>>>Email sent result'+r);
        } catch (VisualforceException e) {
           pdfContent = Blob.valueOf('Exception to create the Payment Receipt PDF');
        }