Search code examples
pdfsalesforceemail-attachmentsvisualforceapex

Attachment is blank when sent first time but has data when sent second time through Apex contoller - SFDC NPSP


I am working on a requirement where I have to send pdf as an attachment through Apex class in SFDC NPSP. So following is my program structure –

VisualForcePage1 has two inputs – input1 and input2. The page also has 3 buttons – Preview, Send Email and Cancel. Depending on values of input1 and input2, ApexClass1 computes values for say output1, output2, output3, output4 and output5 using method getOutputMethod(). Values of these output1...5 variables are stored in a custom object say Custom_Object__c.

public void getOutputMethod() {  
   // calculate values of output1...5  
   // store these values in Custom_Object__c  
}

When user clicks on Preview button, method previewPDF() is called, which in turn calls getOutputMethod(). Output variables (output1...5) are stored in Custom_Object__c and then control is redirected to VisualForcePage2 which has attribute renderAs = ‘pdf’. The generated pdf has accurate data.

When user clicks on Send Email button, method emailPDF() is called, which in turn calls getOutputMethod(). Output variables (output1...5) are stored in Custom_Object__c. However attachment pdf sent in the mail has no data first time. For the same input values, attachment pdf has data when Send Email button is hit second and subsequent trials. Following is the code snippet for sending pdf as email –

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();  
// Reference the attachment page and pass in the account ID  
PageReference pdf =  Page.VisualForcePage2;                         
pdf.getParameters().put('paramater1',input1);   
pdf.getParameters().put('paramater2',input2);            
pdf.setRedirect(true);    
// Take the PDF content    
Blob b = pdf.getContentAsPDF();               
// Create the email attachment    
String filename = 'myPage.pdf';   
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();  
efa.setFileName(filename);  
efa.setBody(b);                 
// Sets the paramaters of the email    
String subject = 'Subject';                                                
body = 'Hello';              
email.setSubject(subject);  
email.setToAddresses('example@email.com');  
email.setPlainTextBody(body);    
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});     
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

ApexClass2 of VisualForcePage2 queries Custom_Object__c with input1 and input2 as parameters as below –

public ApexClass2() {  // constructor  
    var1 = ApexPages.currentPage().getParameters().get('paramater1');  
    var2 = ApexPages.currentPage().getParameters().get('paramater2');               getCustomObject();   
}      

public List<Custom_Object__c> getCustomObject() {  
    List<Custom_Object__c> coList = new List<Custom_Object__c>([
SELECT field1, field2, field3, field4, field5  
FROM Custom_Object__c
WHERE  field1 =: var1
AND    field2 =: var2 ]);     
    return coList;                                                                                         
}    

Please suggest.


Solution

  • Issue resolved. The code which was sending the attachment was picking up the records from database before they were updated. Hence, it was picking either blank or old data.

    I added @future(callout=true) at the start of the method responsible for send attachment mail.