+ Start a Discussion

Multiple attachment created when a Visualforce page is generated as PDF


I have a Visualforcepage which is renderd as PDF and it fires of on a button click on the opportunity. When this page is generated, I create an Attachment Object and attach it to the Opportunity . The problem is every Time for a single button click, Multiple attachment objects are created and attached to the Opportunity. I am using the "action"  attribute of the page element. which I thought, is only called once when the page is first requested from the server, Then Why is it called multiple time?


here is my code for the Visualforce Page



<apex:page standardController="Opportunity" extensions="OpportunityExtension" action="{!Attach}" renderAs="PDF" >


ANd Here is my Controller code for thr Attach() function





//String id = apexpages.currentpage().getparameters().get('id');

PageReference pdf = new PageReference('/apex/Formatted_Quote?id='+apexpages.currentPage().getParameters().get('id'));



attachment = new Attachment();

count = count++;

//pdf = Page.Formatted_Quote;

blob body = pdf.getContent();

attachment.Body = body;

//attachment.IsPrivate = false;

attachment.ParentId = apexpages.currentPage().getParameters().get('id');

attachment.name = 'Quote';

attachment.Body = body;





catch(VisualforceException e){

string msg = 'Error';







return null;



  PageReference Attach() 


The page action does get executed twice for a page that has renderAs="pdf" because of some internal details of how the pdf support is implemented.


I think this is a bug; you might want to open a case with support. 



Meanwhile, as a workaround, can you detect if the attachment already exists and skip the creation code if so?



Did you find a solution for this?




I ran into this same problem myself. I wanted to do a DML update on an object in the PDF page's "action" method. But, because of this double rendering issue, it was not behaving the way I wanted. I logged a case with Salesforce and they acknowledged that it is a bug and that they plan on fixing it in the Summer '11 release. In the mean time, I did come up with a workaround. On the second rendering of the PDF page, there is an additional URL parameter (inline=1). I used this to differentiate between the first and second rendering of the page and was able to check for the presence of this parameter in my page's action method to determine whether or not I should do my DML update operation. Clearly, this is not something that would be supported by Saleforce, and clearly it will break at some point in the future, if/when they ever fix the double rendering issue, but it worked for me.


Salesforce support did suggest a different workaround to me. The following is from Salesforce support:


For now, the possible workaround would be:

Create a separate vf page (not rendered as pdf) with action method that contains the code for incrementing/dml operations and returns the vf page that is rendered as pdf.  The vf PDF page would just display data, not perform processing.



In my case, this workaround was not suitable, because I wanted the rendered PDF to reflect the state of the data BEFORE the DML update. This clearly would do the update first, and then render the PDF, which is not the result I needed.