0

My requirement is to store a VF page rendered as Pdf to Notes and Attachments,My VF Page is being used by a custom button ,and code is as follows :

Visualforce Page :

<apex:page StandardController="Quote__c" showHeader="false"  renderAs="pdf" extensions="QuoteAsPDFController" action="{!attach}"> 

     <apex:stylesheet value="{!URLFOR($Resource.AdvancedPDFResources, 'qstyles.css')}"/> 

    <table width="100%" >
        <tr width="100%">
            <td width="100%" align="left">
                <apex:image value="{!URLFOR($Resource.AdvancedPDFResources, 'logo.gif')}"/>
            </td>
            </tr>
            <tr>
            <td width="100%" align="center">
                 <apex:panelGrid columns="1" width="100%" styleClass="quoteinfo">
                    <b><apex:outputText value="Quote#: {!Quote__c.name}" /></b>
                    <b><apex:outputField value="{!Quote__c.lastmodifieddate}" style="text-align:right"/></b>
                    <!--<apex:outputText value="{!Quote__c.Opportunity_Name__c.Account_Name__c.name}" />-->
                    <apex:outputText value="" />
                </apex:panelGrid>
            </td>
           </tr>

        </table>
                        <b>Prepared For :</b>&nbsp;&nbsp; <apex:outputText value="{!Quote__c.Contact_Name__c}"/><br/>
                        <b>Bill To :</b>&nbsp;&nbsp;<apex:outputText value="{!Quote__c.Bill_To_Name__c}"/><br/>
                        <b>Description : </b>&nbsp;&nbsp;<apex:outputText value="{!Quote__c.Description__c}"/><br/>
                        <b>Status :</b>&nbsp;&nbsp;<apex:outputText value="{!Quote__c.Status__c}"/><br/>
                        <b>Quote Number :</b>&nbsp;&nbsp;<apex:outputText value="{!Quote__c.Quote_Number__c}"/><br/>
                        <b>Phone :</b>&nbsp;&nbsp;<apex:outputText value="{!Quote__c.Phone__c}"/><br/>
                        <b>Email :</b>&nbsp;&nbsp;<apex:outputText value="{!Quote__c.Email__c}"/><br/>
                        <b>Fax :</b>&nbsp;&nbsp;<apex:outputText value="{!Quote__c.Fax__c}"/><br/>    


</apex:page>

Apex Controller :

public with sharing class QuoteAsPDFController {
    public String QuoteId;
    public QuoteAsPDFController(ApexPages.StandardController controller) {
    QuoteId=ApexPages.currentPage().getParameters().get('Id');
    }
    public void attach() {
        Attachment myAttach = new Attachment();
        myAttach.ParentId = QuoteId;//Id of the object to which the page is attached
        myAttach.name = 'Quotation.pdf';
        PageReference myPdf = ApexPages.currentPage();//myPdfPage is the name of your pdf page
        myAttach.body = myPdf.getContentAsPdf();
        insert myAttach;
    }
}

But the code is not working find,and could not identify the function attach().

Error Message :

enter image description here

Pls help.

1
  • What is the error message you receive? Can you please update the question with it?
    – Nick Cook
    Aug 12 '16 at 5:06
1

You need to update page parameter

 action="{!attach()}"

to

 action="{!attach}"

Then it will create the record.

2
  • Its showing Error as :" Too many nested getContent calls" Aug 12 '16 at 5:10
  • @ShobhitSaxena the problem here is you are referring same page from action . So its type of recursive loop situation. So I suggest use another page and then call that page from this page action attribute. Aug 12 '16 at 6:00
1

You're using the action attribute on the apex:page component to create the pdf. As per the docs:

This method will be called before the page is rendered and allows you to optionally redirect the user to another page. This action should not be used for initialization.

From my understanding, this means the constructor won't be called until after attach() has run.

You might have to do this as a 2 stage process:

  1. User clicks a button that takes them to the VF page. This can be thought of as the preview
  2. User then clicks a button on the VF page that calls your attach() method, which then attaches the PDF to the quote
3
  • But a VF page rendered as PDF does not displays Buttons Aug 12 '16 at 5:14
  • The page they go to in step 1 won't be a PDF. It's a regular VF page. I can't think of a way to do this off the top of my head. Just pointing out the problem you're currently facing.
    – Nick Cook
    Aug 12 '16 at 5:17
  • @NickCook constructor will always call first then this attach method. Aug 12 '16 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.