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
kieran.osullivankieran.osullivan 

System.VisualforceException: Too many nested getContent calls in page extension

I have a page which is extended by the "EmailPDFController" class as seen bleow.

The problem is that the getContent call cuases an error and I have no idea why.

 

What I am trying to do is create a pdf and mail it to myself.  Creating the pdf works fine but sending it to myself is not working.

 

See code below.  Try and catch blocks are deliberatly commented out.

 

public with sharing class EmailPDFController {
    public EmailPDFController (ApexPages.StandardController controller){    
        // try {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[]{'firstname.lastname@company.domain'};
            String[] CopyToAddresses = new String[]{'tname@company.domain','firstname.lastname@company.domain'};
            mail.setToAddresses(toAddresses);
            // mail.setCcAddresses(CopyToAddresses);
            mail.setBccAddresses(CopyToAddresses);
            mail.setReplyTo('x@y.com');        
            mail.setSenderDisplayName('x y');
            mail.setSubject('PDF Invoise ');
            // mail.setBccSender(false);
            mail.setUseSignature(false);
            String msg = '<p><b>Hellow</b></p>';


            PageReference congratsPage = ApexPages.currentPage();
            Blob b = congratsPage.getContent();
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName('MyPDF.pdf'); // neat - set name of PDF
            efa.setBody(b); //attach the PDF
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
            
            mail.setHtmlBody(msg);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail});
//        } catch (Exception e){}
    }
    
    @IsTest(SeeAllData=true) public static void testEmailPDFController() {
        ApexPages.StandardController c;
        EmailPDFController EPDFC = new EmailPDFController(c);
        try {
            // System.assertEquals(EPDFC.EmailPDFController(),null);    
        }
        catch (Exception e){}
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
kieran.osullivankieran.osullivan

The solution to this is to create two pages

Page1 is the page that contains the extension, page2 is the pdf page.

Then use the getparameters to get the ID from page1 and pass it to page2.

The get the content of page2. 

 

This fixes the immedout problem but it appears that extensions can't send e-mails see my post

http://boards.developerforce.com/t5/Apex-Code-Development/Can-extensions-to-standard-controllers-send-E-Mails/m-p/498383

 

So it looks like custom controller time.

All Answers

jd123jd123
kieran.osullivankieran.osullivan

The solution to this is to create two pages

Page1 is the page that contains the extension, page2 is the pdf page.

Then use the getparameters to get the ID from page1 and pass it to page2.

The get the content of page2. 

 

This fixes the immedout problem but it appears that extensions can't send e-mails see my post

http://boards.developerforce.com/t5/Apex-Code-Development/Can-extensions-to-standard-controllers-send-E-Mails/m-p/498383

 

So it looks like custom controller time.

This was selected as the best answer