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
ChrisludoviceChrisludovice 

too many nested getContentCalls (VF as PDF)

Hi Experts,

I am creating a custom button and when clicked, it generates the PDF and saves it in attachment but am encountering an error:
Too many nested getContent calls

Below is my Apex extension for the VF:

public class attachPDFToAccount {
    
    private final Account a; //Account object
    public String pdfContent = '<h1>Lorem Ipsum</h1><h4>"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."</h4><h5>"There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."</h5>';
    
     //constructor
    public attachPDFToAccount(ApexPages.StandardController standardPageController) {
        a = (Account)standardPageController.getRecord(); //instantiate the Account object for the current record
    } 
    
    //method called from the Visualforce's action attribute
    public PageReference attachPDF() {
        
        //generate and attach the PDF document
        PageReference pdfPage = new PageReference('/apex/GenerateSOA'); //create a page reference to our pdfDemo Visualforce page, which was created from the post https://interactiveties.com/blog/2015/render-visualforce-pdf.php
        Blob pdfBlob; //create a blob for the PDF content
        if (!Test.isRunningTest()) { //if we are not in testing context
            pdfBlob = pdfPage.getContent(); //generate the pdf blob
        } else { //otherwise, we are in testing context and getContent() gets funky so create the blob manually
        pdfBlob = Blob.valueOf(pdfContent);
        }
        Attachment attach = new Attachment(parentId = a.Id, Name = 'accSOA -' + DateTime.now().formatLong() + '.pdf', body = pdfBlob); //create the attachment object
        insert attach; //insert the attachment
        
        //redirect the user
        PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view(); //we want to redirect the User back to the Account detail page
        pageWhereWeWantToGo.setRedirect(true); //indicate that the redirect should be performed on the client side
        return pageWhereWeWantToGo; //send the User on their way
    }

}

Here's my VF:

<apex:page action="{!attachPDF}" extensions="attachPDFToAccount" standardController="Account">
<!--
    Created by: Chris Ludovice
    Last Update: 10 May 2019 by Chris Ludovice
    
    Notes:
        - Visualforce page for creating a PDF file and attaching to Account
-->
    <apex:pageMessages ></apex:pageMessages><!-- included for display of errors should they occur -->
    <apex:detail inlineEdit="true" relatedList="true"></apex:detail> <!-- included so Account detail information is visible when errors occur -->
</apex:page>


Thanks.
Regards,
Chris L.
ChrisludoviceChrisludovice
User-added image
Raj VakatiRaj Vakati
Since you are using getContent() method in the constructor and calling the same page with same controller again from constructor, it causes recursive execution .. 

Easy way to fix this .. create a another VF page and call this PDF creating from the another VF page 

https://salesforce.stackexchange.com/questions/83275/too-many-nested-getcontent-calls
https://salesforce.stackexchange.com/questions/83275/too-many-nested-getcontent-calls/83290#83290
https://blog.jeffdouglas.com/2010/07/16/create-and-email-a-pdf-with-salesforce-com/
http://salesforce.questionidea.com/questions/46220/dynamically-generated-vfp-attach-to-the-case-too-many-content-calls