You need to sign in to do that
Don't have an account?
Laura Boyd 18
Using AddFields method for multiple objects
Hi,
I have a VF page which displays values from multiple objects (Purchase_Order__c (parent), Items (child of parent), Supplier (lookup from purchase order) etc.
I have written a controller which generates a PDF based on the above template and emails it to the specified email address. This works fine.
The issue I have is that I have used the addfields method to provide access to the Purchase Order object fields which works perfectly but I now need to add in access to the other objects fields. I presumed I could just repeat the addfields method put keep getting error of: You cannot call addFields after you've already loaded the data. This must be the first thing in your constructor
Controller:
public class SendEmail{
public SendEmail(ApexPages.StandardController controller) {
controller.addFields(new List<String>{'Name', 'Annual_Contract__c', 'Date_Raised__c', 'Total__c', 'Justification__c'});
Purchase_Order__c po=(Purchase_Order__c) controller.getRecord();
}
//Set variable values
public String EmailAddress {get;set;}
public String eSubject{get;set;}
public String eBody{get;set;}
//Start mail process and set criteria for PDF
public PageReference SendEmail() {
Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();
PageReference pdfGeneration=Page.Purchase_Order_PDF;
pdfGeneration.SetRedirect(true);
Blob b=pdfGeneration.getContent();
//Create attachment and set criteria
Messaging.EmailFileAttachment attach=new Messaging.EmailFileAttachment();
attach.setFileName('AttachmentEmailFile.pdf');
attach.setBody(b);
email.SetSubject(eSubject);
email.SetPlainTextBody(eBody);
email.setToAddresses(new String[] { EmailAddress });
email.setFileAttachments(new Messaging.EmailFileAttachment[] {attach});
//Now Sending the Email
Messaging.SendEmailResult[] r=Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
return null;
}
}
VF Page:
<apex:page standardController="Purchase_Order__c" renderAs="pdf" applyBodyTag="false">
<head>
<style type="text/css" media="print">
@page {
@bottom-center {
content: "Company Address: {!$Organization.Name}, {!$Organization.Street}, {!$Organization.City}, {!$Organization.PostalCode}, {!$Organization.Phone}";
font-size: 10pt;
}
}
* {
margin: 0px;
padding: 0px;
font-family: Arial Unicode MS;
font-size: 10pt;
}
div.logoheader {
background: url("{!$Resource.Logo}") no-repeat left;
margin-top: 15px;
height: 130px;
width: 300px;
position: running(header);
}
div.poblock {
float:right;
text-align: right;
font-size: 10pt;
margin-top: 15px;
}
.tableHead {
border-width: 2px 0px 2px 0px;
border-color: #000;
border-style: solid;
text-align: center;
padding: 5px;
background-color: #eee;
margin-top: 60px;
}
.tablebody {
border-width: 1px;
border-color: #000;
border-style: solid;
text-align: center;
padding: 5px
}
div.footer {
display: block;
padding: 5px;
position: running(footer);
text-align: center;
margin-bottom: 15px;
font-size: 10pt;
}
#totalCost {
margin-top: 15px;
margin-right: 25px;
text-align: right;
}
#totalCostLabel {
font-weight: bold;
margin-right: 65px;
text-align: right;
}
</style>
</head>
<table width="100%" style="margin-top: 5px;">
<tr width="100%">
<td width="30%" align="left">
<apex:image id="theImage" value="{!$Resource.Logo}" width="200px"/>
</td>
<td width="30%" align="center">
</td>
<td width="40%" align="right">
<p style="text-align:left; font-size: 30pt; font-weight: bold; vertical-align:top;">Purchase Order</p>
<div class="poblock">
<apex:panelGrid columns="3">
<apex:outputLabel value="PO Name: "/><apex:OutputText value="{!Purchase_Order__c.Name}"/><br></br>
<apex:outputlabel value="Date Requested: "/><apex:outputText value="{0,date,dd'/'MM'/'yyyy}"><apex:param value="{!Purchase_Order__c.Date_Raised__c}"/></apex:outputText><br></br>
<apex:outputlabel value="Requester: "/><apex:outputText value="{!Purchase_Order__c.Requester__r.FirstName&" "&Purchase_Order__c.Requester__r.LastName}"/><br></br>
</apex:panelGrid>
</div>
</td>
</tr>
</table>
<table align="center" width="100%" style="margin-top:80px;">
<tr>
<th id="SPDetails" style="border-style: solid; background-color: #eee; padding: 2px; border-width: 3px;">Supplier Details</th>
<th id="Blank"></th>
<th id="ShipTo" style="border-style: solid; background-color: #eee; padding: 2px; border-width: 3px;">Ship To</th>
</tr>
<tr width="100%">
<td width="25%" align="left" style="border-style: solid; border-width: 1px; padding: 3px">
<apex:panelGrid columns="1">
<apex:outputText value="{!Purchase_Order__c.Supplier_Name__r.Name}" />
<apex:outputText value="{!Purchase_Order__c.Supplier_Name__r.Street__c}"/>
<apex:outputText value="{!Purchase_Order__c.Supplier_Name__r.City__c}"/>
<apex:outputText value="{!Purchase_Order__c.Supplier_Name__r.County__c}"/>
<apex:outputText value="{!Purchase_Order__c.Supplier_Name__r.Postcode__c}"/>
</apex:panelGrid>
</td>
<td width="40%" align="center"></td>
<td width="25%" style="border-style: solid; border-width: 1px; padding: 3px;">
<apex:panelGrid columns="1">
<apex:outputText value="{!Purchase_Order__c.Delivery_Contact__c}"/>
<apex:outputText value="{!Purchase_Order__c.Delivery_Addresses__r.Name}"/>
<apex:outputText value="{!Purchase_Order__c.Delivery_Addresses__r.City__c}"/>
<apex:outputText value="{!Purchase_Order__c.Delivery_Addresses__r.County__c}"/>
<apex:outputText value="{!Purchase_Order__c.Delivery_Addresses__r.Postcode__c}"/>
</apex:panelGrid>
</td>
</tr>
</table>
<apex:pageBlock >
<apex:pageBlockSection columns="1">
<apex:pageBlockTable value="{!Purchase_Order__c.items__r}" var="item" columnsWidth="16%,16%,16%,16%,16%,16%" headerClass="tableHead" style="margin-top: 45px;">
<apex:column value="{!item.Name}" styleClass="tablebody"/>
<apex:column value="{!item.Budget_Year__c}" styleClass="tablebody"/>
<apex:column value="{!item.Department_Budget__c}" styleClass="tablebody"/>
<apex:column value="{!item.Product__c}" styleClass="tablebody"/>
<apex:column value="{!item.Quantity__c}" styleClass="tablebody"/>
<apex:column value="{!item.Cost__c}" styleClass="tablebody"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
<div id="totalCost"><span id="totalCostLabel">{!$ObjectType.Purchase_Order__c.Fields.Total__c.Label}:</span> <apex:outputField value="{!Purchase_Order__c.Total__c}"/></div>
<table align="left" width="100%" style="border-style: solid; border-width: 1px; padding: 1.5px; margin-top: 35px;">
<tr>
<td>
<apex:panelGrid columns="1">
<apex:outputLabel value="Comments" rendered="{!IF(Purchase_Order__c.Justification__c <> "", true, false)}"/>
<apex:outputText value="{!Purchase_Order__c.Justification__c}" rendered="{!IF(Purchase_Order__c.Justification__c <> "", true, false)}"/>
</apex:panelGrid>
</td>
</tr>
</table>
</apex:page>
I have a VF page which displays values from multiple objects (Purchase_Order__c (parent), Items (child of parent), Supplier (lookup from purchase order) etc.
I have written a controller which generates a PDF based on the above template and emails it to the specified email address. This works fine.
The issue I have is that I have used the addfields method to provide access to the Purchase Order object fields which works perfectly but I now need to add in access to the other objects fields. I presumed I could just repeat the addfields method put keep getting error of: You cannot call addFields after you've already loaded the data. This must be the first thing in your constructor
Controller:
public class SendEmail{
public SendEmail(ApexPages.StandardController controller) {
controller.addFields(new List<String>{'Name', 'Annual_Contract__c', 'Date_Raised__c', 'Total__c', 'Justification__c'});
Purchase_Order__c po=(Purchase_Order__c) controller.getRecord();
}
//Set variable values
public String EmailAddress {get;set;}
public String eSubject{get;set;}
public String eBody{get;set;}
//Start mail process and set criteria for PDF
public PageReference SendEmail() {
Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();
PageReference pdfGeneration=Page.Purchase_Order_PDF;
pdfGeneration.SetRedirect(true);
Blob b=pdfGeneration.getContent();
//Create attachment and set criteria
Messaging.EmailFileAttachment attach=new Messaging.EmailFileAttachment();
attach.setFileName('AttachmentEmailFile.pdf');
attach.setBody(b);
email.SetSubject(eSubject);
email.SetPlainTextBody(eBody);
email.setToAddresses(new String[] { EmailAddress });
email.setFileAttachments(new Messaging.EmailFileAttachment[] {attach});
//Now Sending the Email
Messaging.SendEmailResult[] r=Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
return null;
}
}
VF Page:
<apex:page standardController="Purchase_Order__c" renderAs="pdf" applyBodyTag="false">
<head>
<style type="text/css" media="print">
@page {
@bottom-center {
content: "Company Address: {!$Organization.Name}, {!$Organization.Street}, {!$Organization.City}, {!$Organization.PostalCode}, {!$Organization.Phone}";
font-size: 10pt;
}
}
* {
margin: 0px;
padding: 0px;
font-family: Arial Unicode MS;
font-size: 10pt;
}
div.logoheader {
background: url("{!$Resource.Logo}") no-repeat left;
margin-top: 15px;
height: 130px;
width: 300px;
position: running(header);
}
div.poblock {
float:right;
text-align: right;
font-size: 10pt;
margin-top: 15px;
}
.tableHead {
border-width: 2px 0px 2px 0px;
border-color: #000;
border-style: solid;
text-align: center;
padding: 5px;
background-color: #eee;
margin-top: 60px;
}
.tablebody {
border-width: 1px;
border-color: #000;
border-style: solid;
text-align: center;
padding: 5px
}
div.footer {
display: block;
padding: 5px;
position: running(footer);
text-align: center;
margin-bottom: 15px;
font-size: 10pt;
}
#totalCost {
margin-top: 15px;
margin-right: 25px;
text-align: right;
}
#totalCostLabel {
font-weight: bold;
margin-right: 65px;
text-align: right;
}
</style>
</head>
<table width="100%" style="margin-top: 5px;">
<tr width="100%">
<td width="30%" align="left">
<apex:image id="theImage" value="{!$Resource.Logo}" width="200px"/>
</td>
<td width="30%" align="center">
</td>
<td width="40%" align="right">
<p style="text-align:left; font-size: 30pt; font-weight: bold; vertical-align:top;">Purchase Order</p>
<div class="poblock">
<apex:panelGrid columns="3">
<apex:outputLabel value="PO Name: "/><apex:OutputText value="{!Purchase_Order__c.Name}"/><br></br>
<apex:outputlabel value="Date Requested: "/><apex:outputText value="{0,date,dd'/'MM'/'yyyy}"><apex:param value="{!Purchase_Order__c.Date_Raised__c}"/></apex:outputText><br></br>
<apex:outputlabel value="Requester: "/><apex:outputText value="{!Purchase_Order__c.Requester__r.FirstName&" "&Purchase_Order__c.Requester__r.LastName}"/><br></br>
</apex:panelGrid>
</div>
</td>
</tr>
</table>
<table align="center" width="100%" style="margin-top:80px;">
<tr>
<th id="SPDetails" style="border-style: solid; background-color: #eee; padding: 2px; border-width: 3px;">Supplier Details</th>
<th id="Blank"></th>
<th id="ShipTo" style="border-style: solid; background-color: #eee; padding: 2px; border-width: 3px;">Ship To</th>
</tr>
<tr width="100%">
<td width="25%" align="left" style="border-style: solid; border-width: 1px; padding: 3px">
<apex:panelGrid columns="1">
<apex:outputText value="{!Purchase_Order__c.Supplier_Name__r.Name}" />
<apex:outputText value="{!Purchase_Order__c.Supplier_Name__r.Street__c}"/>
<apex:outputText value="{!Purchase_Order__c.Supplier_Name__r.City__c}"/>
<apex:outputText value="{!Purchase_Order__c.Supplier_Name__r.County__c}"/>
<apex:outputText value="{!Purchase_Order__c.Supplier_Name__r.Postcode__c}"/>
</apex:panelGrid>
</td>
<td width="40%" align="center"></td>
<td width="25%" style="border-style: solid; border-width: 1px; padding: 3px;">
<apex:panelGrid columns="1">
<apex:outputText value="{!Purchase_Order__c.Delivery_Contact__c}"/>
<apex:outputText value="{!Purchase_Order__c.Delivery_Addresses__r.Name}"/>
<apex:outputText value="{!Purchase_Order__c.Delivery_Addresses__r.City__c}"/>
<apex:outputText value="{!Purchase_Order__c.Delivery_Addresses__r.County__c}"/>
<apex:outputText value="{!Purchase_Order__c.Delivery_Addresses__r.Postcode__c}"/>
</apex:panelGrid>
</td>
</tr>
</table>
<apex:pageBlock >
<apex:pageBlockSection columns="1">
<apex:pageBlockTable value="{!Purchase_Order__c.items__r}" var="item" columnsWidth="16%,16%,16%,16%,16%,16%" headerClass="tableHead" style="margin-top: 45px;">
<apex:column value="{!item.Name}" styleClass="tablebody"/>
<apex:column value="{!item.Budget_Year__c}" styleClass="tablebody"/>
<apex:column value="{!item.Department_Budget__c}" styleClass="tablebody"/>
<apex:column value="{!item.Product__c}" styleClass="tablebody"/>
<apex:column value="{!item.Quantity__c}" styleClass="tablebody"/>
<apex:column value="{!item.Cost__c}" styleClass="tablebody"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
<div id="totalCost"><span id="totalCostLabel">{!$ObjectType.Purchase_Order__c.Fields.Total__c.Label}:</span> <apex:outputField value="{!Purchase_Order__c.Total__c}"/></div>
<table align="left" width="100%" style="border-style: solid; border-width: 1px; padding: 1.5px; margin-top: 35px;">
<tr>
<td>
<apex:panelGrid columns="1">
<apex:outputLabel value="Comments" rendered="{!IF(Purchase_Order__c.Justification__c <> "", true, false)}"/>
<apex:outputText value="{!Purchase_Order__c.Justification__c}" rendered="{!IF(Purchase_Order__c.Justification__c <> "", true, false)}"/>
</apex:panelGrid>
</td>
</tr>
</table>
</apex:page>