[SalesForce] How to save a VF page rendered as PDF to Notes and Attachments

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.

Best Answer

You need to update page parameter

 action="{!attach()}"

to

 action="{!attach}"

Then it will create the record.

Related Topic