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
Ryan DRyan D 

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!

Best Answer chosen by Admin (Salesforce Developers) 
Ryan DRyan D

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

jungleeejungleee

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 :

 

        //Call your VF page
// WIth out any parameters to the vfpage.
pageReference pdf = Page.YOURVFPAGE;
//With paramters
PageReference pdf = new PageReference('/apex/SamplePdfDoc?Id='+theParameter); Attachment attach = new Attachment(); Blob body; //the getContent()method cannot used in Trigger, but there is a workaround which is a little tricky. body = pdf.getContent(); attach.Body = body; attach.Name = 'pdfName.pdf'; attach.IsPrivate = false; attach.ParentId = 'opprtunityID'; insert attach;

 Hope this helps.

 

-ಸಮಿರ್

 

Ryan DRyan D

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):

 

public void savePdf() {
 
    PageReference pdf = new PageReference('apex/MYPAGE?id='+CHILDID);
 
    // create the new attachment
    Attachment attach = new Attachment();
 
    // the contents of the attachment from the pdf
    Blob body;
   
    try {
        // returns the output of the page as a PDF
    	body = pdf.getContent();
 
    // need to pass unit test -- current bug	
    } catch (VisualforceException e) {
    	body = Blob.valueOf('Some Text');
    }
 
    attach.Body = body;
    // add the custom file name
    attach.Name = pdfName;
    attach.IsPrivate = false;
    // attach the pdf to the account
    attach.ParentId = parentId;
    insert attach;
 
  }

 

Can somebody help me with the error code I am getting??

 

Thanks for the help:)

 

 

Ryan DRyan D

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

Ryan DRyan D

The Saga continues:

I found the following code: 

PageReference pageRef = ApexPages.currentPage();

This code is awesome because when I plugged it into my savePDF() method instead of:

PageReference pdf = new PageReference('apex/ProposalTestPage2?id='+recordId);

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

Ryan DRyan D

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.

This was selected as the best answer
ankur khattriankur khattri
hey Ryan D,
                    can u show me the code(visualfroce and controller)? m not able to get it 

thanks,
Ankur