You need to sign in to do that
Don't have an account?
Attach pdf to standard object from VF page with a custom controller
I have a bit of a tricky scenario.
I have a custom VF page to display information about a custom object. The page uses a custom controller. The custom object is the child of Opportunity. The VF page is rendered as PDF. What I want to accomplish is this: Insert the PDF page as an Attachment on the Opportunity that the custom object is related to. Does anybody know the best/easiest way to do this? I'm a bit of a Salesforce/VisualForce noob so any help is greatly appreciated.
Thanks!
Just to close off this topic: the reason I was getting five copies was that I was calling getContent from a method inside the page I was rendering as pdf. this was basically creating a loop until errored out on the fifth copy. the solution is to have a separate controller that calls getContent on the page that is being rendered as pdf.
All Answers
Hi Ryan,
What is the scenario in which you would want to insert the vf page as an attachement, I can think of 2 as of now:
1.) There's a button on the child object , click on it and that will add the vf page to the Opportunity.
2). On creation of the child object, you want to attach the pdf to the opportunity(thru a trigger).
The 1st option is a tad easy to achive while the 2nd is very tricky.
Anyways for the both the mehtods the main code snippet is :
Hope this helps.
-ಸಮಿರ್
The scenarios in which I would want to do this is: the client wants a PDF printout of the VF page (rendered as pdf) that displays a bunch of Info about the child object. They want to generate it, look at it, print it, and have it auto attach to the Opportunity object in "notes and attachments" related list.
Anyway, the code works, HOWEVER, I was not able to open the file that got attached to the Opportunity (see the error below):
ERROR: Adobe Reader could not open 'FILENAME.pdf' because it is either not a supported file type or because the file has been damaged (for, example, it was sent as an email attachment and wasn't correctly decoded).
I actually had most of this code already as I had found it with Google-Fu. I just had to make some small changes and I now have the file attaching to the parent object "opporunity".
Here is the code that inserts the PageReference (savePdf lives in my custom controller and gets called from my init() method after loading up all the data into the VF page):
Can somebody help me with the error code I am getting??
Thanks for the help:)
I did some further investigation with this problem. I opened the file with Notepad++ and the contents are 'Some text', so the getContent() must be failing. Does anyone have any insight into this or how I can proceed with troubleshooting.
Thanks.
RD
The Saga continues:
I found the following code:
This code is awesome because when I plugged it into my savePDF() method instead of:
It properly inserts the file into my Opportunity. The problem now is that whenever I click on the link that takes me to my renderAs pdf VisualForce page, then go back into my Opportunity, I see that it actually attached 5 identical copies of the file to my opportunity. Any thoughts on this??
RD
Just to close off this topic: the reason I was getting five copies was that I was calling getContent from a method inside the page I was rendering as pdf. this was basically creating a loop until errored out on the fifth copy. the solution is to have a separate controller that calls getContent on the page that is being rendered as pdf.
can u show me the code(visualfroce and controller)? m not able to get it
thanks,
Ankur