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
TehNrdTehNrd 

Issues attaching PDF to Opportunity

So I have two pages that share the same controller. On the first page you enter you inputs and the second page rendered as a PDF has all of you outputs. Before Spring '09 something like this wouldn't have worked.

It now works fine if you direct a user straight to that page but if you try to get the content of the page and turn it into an attachment it does not work.

Here is the code:

Page 1: pdfCreate
<apex:page controller="pdfIssues">
<apex:form >
<apex:inputText value="{!yourName}"/><br/>
<apex:commandButton value="View PDF" action="{!view}"/>
<apex:commandButton value="Attach PDF" action="{!attach}"/>
</apex:form>
</apex:page>

Page 2: pdfView
<apex:page controller="pdfIssues" renderAs="pdf">
<apex:outputText value="{!yourName}"/><br/>
</apex:page>

Controller:
public class pdfIssues {

public String yourName {get; set;}

public PageReference view() {
PageReference pdf = Page.pdfView;
return pdf;
}

public PageReference attach() {
Id oppID = [select Id from Opportunity limit 1].Id;

PageReference pdf = Page.pdfView;
Blob pdfContent = pdf.getContent();

Attachment doc = new Attachment(
ParentId = oppID,
Body = pdfContent,
Name = yourName +'.pdf',
ContentType = 'application/pdf'
);
insert doc;

return new PageReference('/'+doc.Id);
}
}

If you click the  View PDF button everything works great, controller state is maintained and the PDF is rendered successfully. If you clikc the Attach PDF button you will be returned to the attachment but when you try to view it there will be an error.

This is a simplified example but in my more complex page this is some additional strange behavior. It will execute the Attach() method 4 times and insert four PDFs, non of which can be opened.

 

-Thanks,

Jason

Message Edited by TehNrd on 02-04-2009 11:31 AM
Best Answer chosen by Admin (Salesforce Developers) 
dchasmandchasman

Information I got from support included a stack trace (I now suspect it had nothing to do with your stuff at all) that clearly showed a failure because of binding to a static property.

 

The fix for the 4 attachment issue was applied to cs0/cs2/na1/na6 last night.

All Answers

TehNrdTehNrd
I've opened a support case. Case # 02408895.
dchasmandchasman
We are looking into this - no problem repro'ing the issue - thanks for the excellent repro example. The underlying issue is a combination of errors - one of which is "Too many Apex request on server for this organization, please try later" which I am researching just what that means now...
Update: The fix for the wacky 4 attachment part has been deployed to all instances running Spring '09. The other issue behind this is binding to a static property in your controller (not something I realized we even supported until a few days ago):

public static String yourName {get; set;}

Does this really need to be static? I will have the fix for static property binding in later this week but I expect that using static properties in this case is not required or even desirable? Using a static if you have more than one instance of the class is going to get you into some interesting problems w.r.t. value binding and if you only have a single instance per request using a static is going to be pretty much the same thing as an instance property...

Message Edited by dchasman on 02-08-2009 09:11 PM
TehNrdTehNrd

I'm not sure what makes you think I am binding to a static property. My example above has no static properties.

Thanks for patching the 4 attachment issue as I don't believe my example above fully reproduced this behavior.

I also just tested the example above on our cs2 sandbox and it works great. Not sure if it's fixed in prod yet but thanks for fixing this so quickly!

dchasmandchasman

Information I got from support included a stack trace (I now suspect it had nothing to do with your stuff at all) that clearly showed a failure because of binding to a static property.

 

The fix for the 4 attachment issue was applied to cs0/cs2/na1/na6 last night.

This was selected as the best answer