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>