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
LakshmanLakshman 

Too many nested getContent calls

Hi All,

 

I have a VFP within which I have written:

Visualforce page Name: MyAttach:

<apex:page standardController="CustomObj__C" extension="MyClass" action="saveAttach">

//here I generate PDF data with the help of both standard controller and its extension

</apex:page>

 

in MyClass I have written this method as:

 

public void saveAttach()

{

    System.debug('Inside saveAttach');
    PageReference pdfPage = Page.MyAttach;
    pdfPage.getParameters().put('id',q.id);//q is variable of CustomObj__c which fetches records.
    Blob pdfBlob ;//= pdfPage.getContent();
    pdfBlob = pdfPage.getContent();
    Attachment a = new Attachment(parentId = q.id, name=q.id + '.pdf', body = pdfBlob);
    insert a;
    pdfBlob = Blob.valueOf('Unit Test Attachment Body');?

}

 

When I click a custom button(having MyAttach VFP) from CustomObj__c page layout, it give me error Too many nested getContent calls, I don't know why is this going in infinite loop.

Any Ideas/thoughts/workarounds to this are highly appreciated.

Thank you!

:

Regards,

Lakshman

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

You mean the PDF is not showing the values from standard controller or the properties from extension?

 

If yes then let me tell you one thing that when you call "PageReference pdfPage = Page.MyAttach_V2;" it will be a fresh call to that page and controller will be refreshed. So make sure that you will pass the Id of object which is used as standard controller while creating a PageReference.

 

Hope this will help.

 

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

All Answers

aballardaballard

looks like the page ivoke the action SaveAttach, which loads the page as a pdf, which invokes the action saveAttach which loads the page as a pdf which ovokes the action saveAttach...

LakshmanLakshman

Thanks for your response.

So how do we resolve it?

Any thoughts/workarounds?

 

Regards,

Lakshman

Ankit AroraAnkit Arora

When you hit "MyAttach" page, after load it calls "saveAttach" method which again call the "MyAttach" page (PageReference pdfPage = Page.MyAttach;). So it will apparently create an infinite loop.

 

To avoid this create another VFP which will be the clone of "MyAttach" lets say "MyAttach_V2". Just remove the action from "MyAttach_V2" and when you hit "MyAttach" it will call "saveAttach" method. In that method just replace this line :

 

PageReference pdfPage = Page.MyAttach;

 

with this :

 

PageReference pdfPage = Page.MyAttach_V2;

 

Hope this is clear to you, and if not then let me know.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

LakshmanLakshman

Hi Ankit,

 

Thanks for your reply.

This is what exactly I have done, but the problem now is that two same attachments are getting inserted.

My new page looks like:

<apex:page standardController="CustomObj__C" extension="MyClass" action="saveAttach">

//here I generate same as older page PDF data with the help of both standard controller and its extension

</apex:page>



My older page looks same except action method removed:

 

<apex:page standardController="CustomObj__C" extension="MyClass">

//here I generate PDF data with the help of both standard controller and its extension

</apex:page>



Dont know why it inserts two attachment. Please let me know if you have any thoughts to this.

Thank you!

 

Regards,

Lakshman

Ankit AroraAnkit Arora

Can you please post code snippet of both pages with name and controller. As I am pretty sure "saveAttachment" is called twice.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

LakshmanLakshman

The code is too long so I am giving brief summary of it:

 

I have a class as follows:

 

public class Myclass
{
public ApexPages.StandardController controller;
//this class is extension of My original MyAttach page and its replica MyAttach_V2.
public Myclass(ApexPages.StandardController c) 
{
   controller = c;
}
//following method is used to insert attachment to object
public void saveAttach()
{
    System.debug('Inside saveAttach');
    PageReference pdfPage = Page.MyAttach_V2;
    //putting id of custom object in parm id so that I can read it to fetch records
    pdfPage.getParameters().put('id',q.id);//q is variable of CustomObj__c which fetches records.
    Blob pdfBlob ;//= pdfPage.getContent();
    if(!Test.isRunningTest())
    {
pdfBlob = pdfPage.getContent(); Attachment a = new Attachment(parentId = q.id, name=q.id + '.pdf', body = pdfBlob); insert a;
}
 else
    { 
     pdfBlob = Blob.valueOf('Unit Test Attachment Body');
    }
 } }

 

 

Apex Page: MyAttach

<apex:page standardController="CustomObj__c" extension="MyClass" action="{!saveAttach}">

//here I generate PDF data with the help of both standard controller and its extension

</apex:page>

 

Apex Page:MyAttach_V2

<apex:page standardController="CustomObj__c" extension="MyClass" >

//same content as MyAttach page

</apex:page>



Now when I click Generate PDF, I get two attachments save in the Attachments section.

The button Generate PDF is custom button of CustomObj__c and has VFP MyAttach in it.

 

 

I have search for method saveAttach in my env. I found only two occurrences one in class and other one in MyAttach page.

Let me know if I am missing something.

Thank you!

 

Regards,

Lakshman



Ankit AroraAnkit Arora

I have tried the same scenario in my org for you and successfully saved the attachment only once. Here the code of my two pages and class. To solve your problem I just removed "renderAs="pdf" " from MyAttach page.

 

VFP : MyAttach (Notice that there is not renderAs written on this page)

 

<apex:page standardController="Account" action="{!saveAttach}" extensions="test">
    <apex:outputLabel value="PDF Saved Successfully"/>
</apex:page>

 

VFP : MyAttach_V2

 

<apex:page renderAs="PDF">
    <apex:outputLabel value="My PDF Page"/>
</apex:page>

 

Class :

 

public class test
{

    public test(ApexPages.StandardController controller)
    {
    }
    
    public void saveAttach()
    {
        PageReference pdfPage = Page.MyAttach_V2;
        Blob pdfBlob = pdfPage.getContent();
        Attachment a = new Attachment(parentId = ApexPages.currentPage().getParameters().get('id'), name='Astala' + '.pdf', body = pdfBlob);
        insert a ;
    }
}

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

LakshmanLakshman

Thanks for your efforts.

I tried your way, now it stores only one attachment, only problem is that our user needs to have a view to the attachment also as a PDF. So is there any way to do this.

 

Regards,

Lakshman

Ankit AroraAnkit Arora

Indeed you can.

 

Here is the code :

 

VFP : MyAttach

 

<apex:page standardController="Account" action="{!saveAttach}" extensions="test" showHeader="false" sidebar="false">
    <apex:iframe height="100%" src="/apex/MyAttach_V2"/>
</apex:page>

 

VFP : MyAttach_V2

 

<apex:page renderAs="PDF">
    <apex:outputLabel value="My PDF Page"/>
</apex:page>

 Class is same. I know it will work :)

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

LakshmanLakshman

Yes it works, but the pdf has only static content in it. Dynamic content is lost which was coming from controller(MyClass)

 

Regards,

Lakshman

Ankit AroraAnkit Arora

You mean the PDF is not showing the values from standard controller or the properties from extension?

 

If yes then let me tell you one thing that when you call "PageReference pdfPage = Page.MyAttach_V2;" it will be a fresh call to that page and controller will be refreshed. So make sure that you will pass the Id of object which is used as standard controller while creating a PageReference.

 

Hope this will help.

 

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

This was selected as the best answer
LakshmanLakshman

Both standard controller values and properties value are not shown. Wait let me try what you said.

 

Regards,

Lakshman

LakshmanLakshman

Cheers Ankit!!!

It works.

Thanks a ton Ankit. For others, I added this code in my VFP page MyAttach

 

<apex:iframe height="100%" src="/apex/MyAttach_V2?id={!$CurrentPage.parameters.Id}"/>

 

Regards,

Lakshman

Ankit AroraAnkit Arora

Glad that this is resolved :)

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Sforce ChrisSforce Chris
was having the same issue, thanks for the nice idea @Ankit_Arora.

However, there is a better fix for the apex:iframe. The height never scales correct so that's not great. Instead, you should use an apex:include.

In your example, it would be: <apex:include pageName="MyAttach_V2"/>
Amit Kumar 87Amit Kumar 87
hi 

i was getting this issue..i made changes as you suggested but when i open my attached pdf it is blank. Only field names(static) are there no field value(Dyanamic).

Plz suggest asap
Ankit Arora 30Ankit Arora 30
@Everyone

I want to convert VF to PDF and then save it to Notes and attachments and below are the VF pages and apex class.

When I run the process, a pdf is created successfully at the right location under the Custom__c but its blank. When I "login to community as a user", again the same vf page is blank but when I create a custom link of vf page in Custom__c object, I see the entire letter in pdf. Before putting the extension and action tag in VF Page, I was able to see the same page from the custom link and from "login to community as a user". Can you please tell me what I am doing wrong, why its showing blank page.
Also VF page render the output dynamically, its not a static page.
Please help me.

1st Visualforce Page: (Letter)
<apex:page standardcontroller="Custom__c" extensions="attachPDFToCustom" action="{!attachPDF}" standardStylesheets="false" showHeader="false" applyHtmlTag="false" applyBodyTag="false" docType="html-5.0" >
<apex:include pageName="Letter_V2"/>

2nd Visualforce Page: (Letter_V2)
<apex:page renderAs="pdf"> </apex:page>

Apex Class:
public class attachPDFToCustom {

public attachPDFToCustom(ApexPages.StandardController standardPageController) {
    }
 
public void attachPDF() {
PageReference pdfPage = Page.Letter_V2;
Attachment attach = new Attachment();
Blob pdfBlob;
      try {
             pdfBlob = pdfPage.getContent();
           }
       catch (VisualforceException e) {
             pdfBlob = Blob.valueOf('Some Text');
           }
                
attach.parentId = ApexPages.currentPage().getParameters().get('id');
attach.Name = 'Letter - '+ system.Now() + ' .pdf';
attach.body = pdfBlob;
insert attach;
    }
}


Thank you

Regards,
Ankit