0

here is my scenario.

From a method 'myClass1' I send Account, contact and a list of custom records to a method 'createEmailwithPDF' of another class - 'myClass2'.

'createEmailwithPDF' takes all the inputs and populates it to a VF page. The page is then rendered as pdf and attached to a mail. This method returns the created email back to 'myClass1'.

But, I'm not able to populate the values to VF page from 'createEmailwithPDF'.

I have simplified the scenario for better understanding of the problem.

-------------------CONTROLLER------------------------

    public class myClass2{

    public string firstName {get; set;}
    public string lastName {get; set;}
    public string Address1 {get; set;}
    public string Address2 {get; set;}
    public string Address3 {get; set;}
    public date dateValue {get; set;}
    public List<Custom__c> Contracts {get; set;}


    public void createEmailwithPDF (String fN, String lN, String A1, 
                                   String A2, List<custom__c> con)
{

        dateValue = system.today(); 
        firstName =fN;
        lastName = lN;
        Address1 = A1;
        Address2 = A2;
        contracts = con;

        pagereference pg = new pageReference('/apex/myPage');
        pg.setRedirect(false);
        blob b = pg.getContentAsPdf();

        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
        efa1.setFileName(Address1+'.pdf' );
        efa1.setBody(b);

        email.setSubject('Notification' );
        email.setToAddresses( new String[]{'test@test.com'}  );
        email.setPlainTextBody('Test Body');
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1});
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new 
        Messaging.SingleEmailMessage[] {email});
    } 
}

The VF Page uses the same 'myClass2' controller. It Just maps the Assigned variables.

When I call the 'CreateEmailWithPDF' method from Anonymous window, I am able to get an email with the pdf attachment. But all the variables are assigned as null. the values that I send from the anonymous window are not getting assigned.

I vaguely understand that I need to populate the values in constructor itseld for the varaiables to get assigned. But in my case, I can't do that because my method is getting varaiables from another class.

Please help me with the situation.

Thanks in advance!

  • Your createEmailwithPDF is declared with void so it won't return anything. You need to specify a data type that you want to return. See documentation – David Cheng Sep 2 '19 at 18:34
  • @DavidCheng , thanks for your reply. My issue is not with the method not retuening anything. I just modified the code for better understanding. The issue is that the values that I am passing through the anonymous block is not getting populated in the VF page. – Naveen Karthick Sep 3 '19 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.