0

Im trying to save a Vf page as an Attachment but the blob does not contain the values from the record.

Im trying to understand if i need to pass in the record ID or not.. or what am i missing.

Code Below. Thanks in advance.

    public class ContractAttachment {
    
    private final CR_Contract__c con; 
    
    public ContractAttachment(ApexPages.StandardController standardPageController) {
        con = (CR_Contract__c)standardPageController.getRecord(); 
    }
    

    public PageReference attachPDF() {

        PageReference pdfPage = Page.CRcontract; 
        Blob pdfBlob = pdfPage.getContent();  
        
        Agreement__c agreement = new Agreement__c(CR_Contract__c = con.Id );
        insert agreement;
        
        Attachment attach = new Attachment(parentId = agreement.Id, Name = 'pdfAttachment.pdf', body = pdfBlob); 
        insert attach; 


        PageReference pageWhereWeWantToGo = new ApexPages.StandardController(agreement).view(); 
        pageWhereWeWantToGo.setRedirect(true); 
        return pageWhereWeWantToGo; 
    }}

The VF page that is linked to a custom button

    <apex:page action="{!attachPDF}" extensions="ContractAttachment" standardController="CR_Contract__c">

    <apex:pageMessages ></apex:pageMessages>
    <apex:detail inlineEdit="true" relatedList="true"></apex:detail> 
</apex:page>

And the VF page that i want as an attachment:

    <apex:page standardController="CR_Contract__c"  extensions="CrContractExt" renderAs="pdf" showHeader="false" sidebar="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false">
    <html>
        <head>
        </head>
        <body>
            <div class="container">
                <div>
                    <img class="center" src="{!$Resource.crLogo}" width="15%"/>
                    <h1 class="text-center nobtmargin">Tenancy Agreement (Fixed Term)</h1>
                    <h2 class="text-center">Residential Letting of an Assured Shorthold Tenancy Under Part 1 of the Housing Act 1988 as amended 1996</h2>
                </div>
                <hr/>
                
                <div style="page-break-after:always;">
                    <table width="100%">
                        <tr>
                            <td class="tdfont bottompad bold">Binding Date</td>
                            <td class="tdfont bottompad">
                                <apex:outputText value="{!Day(TODAY())}"></apex:outputText>/
                                <apex:outputText value="{!Month(TODAY())}"></apex:outputText>/
                                <apex:outputText value="{!Year(TODAY())}"></apex:outputText>
                            </td>
                        </tr>
                        <tr>
                            <td class="tdfont bottompad bold">Executed on</td>
                            <td class="tdfont bottompad">Date Shown in the Audit Trail</td>
                        </tr>
                        <tr> 
                            <td class="tdfont bottompad bold" style="vertical-align:top;">Name and address of Landlord(s)</td>
                            <td class="tdfont bottompad">
                                <apex:repeat var="lp" value="{!landlordParties}" >
                                    {!lp.Name__c}<br/><br/>
                                    {!lp.Account__r.BillingStreet}<br/>
                                    {!lp.Account__r.BillingCity}<br/>
                                    {!lp.Account__r.BillingPostalCode}<br/><br/>
                                </apex:repeat>
                            </td>
                        </tr>
                        <tr>
                            <td class="tdfont bottompad bold" style="vertical-align:top;">Name and address of Tenant(s)</td>
                            <td class="tdfont bottompad">
                                <apex:repeat var="tp" value="{!tenantParties}" >
                                    {!tp.Account__r.Name}<br/><br/>
                                    {!tp.Account__r.BillingStreet}<br/>
                                    {!tp.Account__r.BillingCity}<br/>
                                    {!tp.Account__r.BillingPostalCode}<br/><br/>  
                                </apex:repeat>
                            </td>
                        </tr>
                        <tr>
                            <td class="tdfont bottompad bold" style="vertical-align:top;">Name and address of Guarantor(s)</td>
                            <td class="tdfont bottompad">
                                <apex:repeat var="gp" value="{!guarantorParties}" >
                                    {!gp.Account__r.Name}<br/><br/>
                                    {!gp.Account__r.BillingStreet}<br/>
                                    {!gp.Account__r.BillingCity}<br/>
                                    {!gp.Account__r.BillingPostalCode}<br/><br/>  
                                </apex:repeat>
                            </td>
                        </tr>
                        <tr>
                            <td class="tdfont bottompad bold" style="vertical-align:top;">Address of Property</td>
                            <td class="tdfont bottompad">{!CR_Contract__c.Property__r.Mailing_Street__c}<br/>
                                {!CR_Contract__c.Property__r.Mailing_City__c}<br/>
                                {!CR_Contract__c.Property__r.Mailing_Postcode__c}</td>
                        </tr>
                        <tr>
                            <td class="tdfont bottompad bold">Start Date</td>
                            <td class="tdfont bottompad"><apex:outputText value="{!Day(CR_Contract__c.Start_Date__c)}"></apex:outputText>/
                                <apex:outputText value="{!Month(CR_Contract__c.Start_Date__c)}"></apex:outputText>/
                                <apex:outputText value="{!Year(CR_Contract__c.Start_Date__c)}"></apex:outputText>
                            </td>
                        </tr>
                        <tr>
                            <td class="tdfont bottompad bold">End Date</td>
                            <td class="tdfont bottompad"><apex:outputText value="{!Day(CR_Contract__c.End_Date__c)}"></apex:outputText>/
                                <apex:outputText value="{!Month(CR_Contract__c.End_Date__c)}"></apex:outputText>/
                                <apex:outputText value="{!Year(CR_Contract__c.End_Date__c)}"></apex:outputText>
                            </td>
                        </tr>
                    </table>  
                </div>
                
                </div>
                
            </div>
        </body>
    </html>
</apex:page>
3
  • What are you trying to capture from the vf? How are you rendering your vf? And which record id are you referring to here? Can you add your vf to the post and modify your question to provide some more clarity?
    – Jayant Das
    Commented May 4, 2018 at 20:23
  • I cant seem to edit it. so ill add it in the comment
    – ridwan
    Commented May 4, 2018 at 20:26
  • @ridwan No, you should be able to edit your question.
    – sfdcfox
    Commented May 4, 2018 at 20:27

1 Answer 1

2

PageReference.getContent and getContentAsPDF creates a new, independent context from the current page. This means that if you want to get the current page's content, you need to provide the same parameters. Fortunately, I'm pretty sure you should be able to use ApexPages.currentPage instead:

public PageReference attachPDF() {
  Blob pdf = ApexPages.currentPage().getContentAsPDF();
  // rest of code here...
5
  • Thanks.. but this returns error of Too many nested getContent calls.
    – ridwan
    Commented May 4, 2018 at 20:43
  • And that I think it should be this class CrContractExt (the extension for the other page) instead of where implemented currently?
    – Jayant Das
    Commented May 4, 2018 at 20:43
  • That Extension just allows multiple child objects values to be used in the VF. not entirely sure what your question is..
    – ridwan
    Commented May 4, 2018 at 20:49
  • @ridwan You can't use this method via action, because it would create infinite recursion. In my trial that I tested, I made it a button to click. If it's a separate page, you'll need to provide the parameters to again, again, because it's a separate context.
    – sfdcfox
    Commented May 4, 2018 at 20:51
  • @ridwan That's exactly what I was trying to say. You are navigating to a different page which has the pdf content. Once you are in that page, you should have the "download" kind of button on that page and handle it within its extension.
    – Jayant Das
    Commented May 4, 2018 at 20:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .