0

I'm using this controller to create a PDF and send it as an attachement. I originally had the PDF/attachment portion working correctly but could only get it to send the email with the address hard coded. I now have it pulling the email from a field but the attachment portion isn't working.

I'm getting the following error:

System.VisualforceException: SObject row was retrieved via SOQL without querying the requested field: Quote.QuoteLineItems Error is in expression '{!SendEmail}' in component in page email_dspquotepdftocontact: Class.email_DSPQuoteLetter.SendEmail: line 13, column 1

I think I understand it but am unsure how to address it. Any help is appreciated.

public with sharing class email_DSPQuoteLetter {
public String eSubject{get;set;}    
public String eBody{get;set;}
public Quote q {get;set;}
public email_DSPQuoteLetter(ApexPages.StandardController stdCon) {
q = [SELECT Id, Email FROM Quote WHERE Id = :stdCon.getRecord().Id];
}
Public PageReference SendEmail() {
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    PageReference PDFQuote = Page.DSPQuoteTemplateAsPDF;
    PDFQuote.setRedirect(true);
    Blob b = PDFQuote.getContent();
    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName('Quote.pdf');
    efa.setBody(b);
    String[] toAddress = new String[]{};
    toAddress.add(q.email);
    email.setSubject(eSubject);
    email.setPlainTextBody(eBody);
    email.settoAddresses(toAddress);
    email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
    Messaging.SendEmailResult[] r = Messaging.SendEmail(new Messaging.SingleEmailMessage[] {email});
    return null;
}
}

Here is the page

<apex:page standardcontroller="Quote" extensions="email_DSPQuoteLetter">
<apex:pageBlock title="Send Quotaion to Contact By Email">
    <apex:form >
        <apex:outputLabel for="Subject" value="Subject">:</apex:outputLabel><br/>
        <apex:inputText size="80" maxlength="80" value="{!eSubject}" id="Subject"/><br/><br/>
        <apex:outputLabel for="Body" value="Body">:</apex:outputLabel><br/>
        <apex:inputTextArea cols="80" rows="8" value="{!eBody}" id="Body"/><br/><br/>

        <apex:commandbutton value="Send Email" action="{!SendEmail}" title="Send Email"/>
        <apex:commandButton value="Back to Quote Record" action="/{!Quote.id}"/>

    </apex:form>
</apex:pageBlock>

PDF Controller. The PDF renders fine as a PDF.

public class DSPQuoteTemplateAsPDFExtension {
ApexPages.StandardController quote;
public quote q {get;set;}
public PageReference DSPQuoteTemplateAsPDF;
public DSPQuoteTemplateAsPDFExtension(ApexPages.StandardController c)
{
    q = (quote)c.getRecord();
    DSPQuoteTemplateAsPDF = c.view();
}
public PageReference attachDSPQuoteTemplateAsPDF()
{
    PageReference DSPQuoteTemplateAsPDF = Page.DSPQuoteTemplateAsPDF;
    DSPQuoteTemplateAsPDF.getParameters().put('id',q.id);
    Blob pdfBlob = DSPQuoteTemplateAsPDF.getContent();
    Attachment a = new Attachment(parentID = q.id, name = 'Quote' + '_' + q.quotenumber + '_' + q.LastModifiedDate + '.pdf', body = pdfBlob);
    insert a;
    return null;
}

}

DSP PDF Template

<apex:page standardController="QUOTE" showHeader="true" renderAs="PDF">
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="View as PDF" action="/apex/DSPQuoteTemplateAsPDF?id={!Quote.id}"/>
<apex:commandButton value="Attach as PDF" action="/apex/DSPQuoteTemplateAsPDFAttach?id={!Quote.id}"/>
<apex:commandButton value="Email to Contact" action="/apex/email_DSPQuotePDFtoContact?id={!Quote.id}"/>
<apex:commandButton value="Back to Quote" action="/{!Quote.id}"/>
</apex:pageBlockButtons>
<html>
<head></head>
<header>
<table width="100%" border="0" bordercolor="#000000" style="border=collapse" cellpading="3" cellspacing="0">
    <tr>
        <td width="60%">
            <apex:image url="/servlet/servlet.ImageServer?id=01537000000IRDp&oid=00D37000000PqpU&lastMod=1439932475000"/>
        </td>
        <td>
            <apex:outputText style="font-family:arial; font-size:13pt; font-weight:bold" value="Diversified Silicone Products"></apex:outputText><br></br>
            <apex:outputText style="font-family:arial; font-size:13pt; font-weight:bold" value="13937 Rosecrans Avenue"></apex:outputText><br></br>
            <apex:outputText style="font-family:arial; font-size:13pt; font-weight:bold" value="Santa Fe Springs, CA  90670  USA"></apex:outputText><br></br>
        </td>
    </tr>
</table> 
</header>  
<body>
    <br></br>
        <apex:outputText style="font-family:arial; font-size:16pt; font-weight:bold" value="Quote"></apex:outputText>
    <br></br>
    <br></br>
<table width="100%" border="0" bordercolor="#000000" style="border=collapse" cellpading="3" cellspacing="0">
    <tr>
        <td width="20%" valign="top" font-face="arial" size="3">
            Quote Number:
        </td>
        <td width="28%" valign="top" font-face="arial" size="3">
            {!Quote.QuoteNumber}
        </td>
        <td width="4%" valign="top" font-face="arial" size="3">             
        </td>
        <td width="20%" valign="top" font-face="arial" size="3">
            Expiration Date: 
        </td>
        <td width="28%" valign="top" font-face="arial" size="3">
            <apex:outputText value="{0, date, MMMM d','  yyyy}">
                <apex:param value="{!Quote.ExpirationDate}" />
            </apex:outputText>
        </td>
    </tr>
        <tr>
            <td width="20%" valign="top" font-face="arial" size="3">
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
            </td>
            <td width="4%" valign="top" font-face="arial" size="3">
            </td>
            <td width="20%" valign="top" font-face="arial" size="3">
                Created Date
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                <apex:outputText value="{0, date, MMMM d','  yyyy}">
                    <apex:param value="{!Quote.CreatedDate}" />
                </apex:outputText>
            </td>
        </tr>
                                <tr>
            <td width="20%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="4%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="20%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
        </tr>
        <tr>
            <td width="20%" valign="top" font-face="arial" size="3">
                Prepared By:
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                {!Quote.CreatedBy.FirstName} {!Quote.CreatedBy.LastName}
            </td>
            <td width="4%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="20%" valign="top" font-face="arial" size="3">
                Email:
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                {!Quote.CreatedBy.Email}
            </td>
        </tr>
        <tr>
            <td width="20%" valign="top" font-face="arial" size="3">
                CSR:
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                {!Quote.Customer_Service_Rep__r.FirstName} {!Quote.Customer_Service_Rep__r.LastName} 
            </td>
            <td width="4%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="20%" valign="top" font-face="arial" size="3">
                CSR Email:
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                {!LEFT(Quote.CSR_Email__c,35)}
            </td>
        </tr>
        <tr>
            <td width="20%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="4%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="20%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
        </tr>
        <tr>
            <td width="20%" valign="top" font-face="arial" size="3">
                Bill To Account:
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                {!Quote.BillingName}
            </td>
            <td width="4%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="20%" valign="top" font-face="arial" size="3">
                Ship To Account
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                {!Quote.ShippingName}
            </td>
        </tr>
        <tr>
            <td width="20%" valign="top" font-face="arial" size="3">
                Bill To ID:
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                {!Quote.Account.Cust_ID__c}
            </td>
            <td width="4%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="20%" valign="top" font-face="arial" size="3">
                Ship To ID:
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                {!Quote.Account.Cust_ID__c}
            </td>
        </tr>
        <tr>
            <td width="20%" valign="top" font-face="arial" size="3">
                Bill To Address:
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                {!quote.BillingStreet}<br></br>
                {!quote.BillingCity}, {!quote.BillingState}  {!quote.BillingPostalCode}  {!quote.BillingCountry}
            </td>
            <td width="4%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="20%" valign="top" font-face="arial" size="3">
                Ship To Address:
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                {!quote.ShippingStreet}<br></br>
                {!quote.ShippingCity}, {!quote.ShippingState}  {!quote.ShippingPostalCode}  {!quote.ShippingCountry}
            </td>
        </tr>
        <tr>
            <td width="20%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="4%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="20%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
        </tr>
        <tr>
            <td width="20%" valign="top" font-face="arial" size="3">
                Contact Name:
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                {!Quote.Contact.FirstName} {!Quote.Contact.LastName}
            </td>
            <td width="4%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="20%" valign="top" font-face="arial" size="3">
                Contact Email:
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                {!Quote.Contact.Email}
            </td>
        </tr>
        <tr>
            <td width="20%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="4%" valign="top" font-face="arial" size="3">
                <br></br>
            </td>
            <td width="20%" valign="top" font-face="arial" size="3">
                Contact Phone: 
            </td>
            <td width="28%" valign="top" font-face="arial" size="3">
                {!Quote.Contact.Phone}
            </td>
        </tr>
    </table>
        <br></br>
        <br></br>
    <apex:outputText style="font-family:arial; font-size:14pt; font-weight:normal" value="Rogers Corporation is pleased to offer the following pricing:"></apex:outputText>
        <br></br>
        <br></br>
            <apex:dataTable width="100%" value="{!quote.QuoteLIneItems}" var="qli" cellpadding="3" border="1" styleClass="tableClass" rowClasses="odd,even" id="LineItems">

                <apex:column style="font-family:arial; font-size:9pt; text-align:center">
                    <apex:facet name="header"> <span style="font-size:10pt; font-family:arial">Part ID</span></apex:facet>
                    <apex:outputField value="{!qli.Product2.ProductCode}"/>
                </apex:column>

                <apex:column style="font-family:arial; font-size:9pt">
                    <apex:facet name="header"> <span style="font-size:10pt; font-family:arial">Part Description</span></apex:facet>
                    <apex:outputField value="{!qli.Product2.Name}"/>
                </apex:column>                    

                <apex:column style="font-family:arial; font-size:9pt; text-align:center">
                    <apex:facet name="header"> <span style="font-size:10pt; font-family:arial">List Price</span></apex:facet>
                    <apex:outputText value="{!qli.ListPrice}"/>
                </apex:column> 

                <apex:column style="font-family:arial; font-size:9pt; text-align:center">
                    <apex:facet name="header"> <span style="font-size:10pt; font-family:arial">Unit of Measure</span></apex:facet>
                    <apex:outputField value="{!qli.UOM2__c}"/>
                </apex:column> 

                <apex:column style="font-family:arial; font-size:9pt; text-align:center">
                    <apex:facet name="header"> <span style="font-size:10pt; font-family:arial">Discount Schedule</span></apex:facet>

                        <Table width="100%" border="1" bordercolor="#000000" style="border=collapse" cellpading="3" cellspacing="0">
                            <tr>
                                <td width="25%" valign="top" font-face="arial" size="3">
                                    MOQ
                                </td>
                                <td width="25%" valign="top" font-face="arial" size="3">
                                    Price
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    {!qli.Tier_1_Quantity__c}
                                </td>
                                <td>
                                    {!qli.Tier_1_Price__c}
                                </td>
                           </tr>
                           <tr>
                                <td>
                                    {!qli.Tier_2_Quantity__c}
                                </td>
                                <td>
                                    {!qli.Tier_2_Price__c}
                                </td>
                           </tr>
                           <tr>
                                <td>
                                    {!qli.Tier_3_Quantity__c}
                                </td>
                                <td>
                                    {!qli.Tier_3_Price__c}
                                </td>
                           </tr>
                           <tr>
                                <td>
                                    {!qli.Tier_4_Quantity__c}
                                </td>
                                <td>
                                    {!qli.Tier_4_Price__c}
                                </td>
                           </tr>
                           <tr>
                                <td>
                                    {!qli.Tier_5_Quantity__c}
                                </td>
                                <td>
                                    {!qli.Tier_5_Price__c}
                                </td>
                           </tr>
                           <tr>
                                <td>
                                    {!qli.Tier_6_Quantity2__c}
                                </td>
                                <td>
                                    {!qli.Tier_6_Price__c}
                                </td>
                           </tr>
                           <tr>
                                <td>
                                    {!qli.Tier_7_Quantity__c}
                                </td>
                                <td>
                                    {!qli.Tier_7_Price__c}
                                </td>
                           </tr>
                           <tr>
                                <td>
                                    {!qli.Tier_8_Quantity__c}
                                </td>
                                <td>
                                    {!qli.Tier_8_Price__c}
                                </td>
                           </tr>
                        </Table>
                </apex:column>   
            </apex:dataTable>    
        <br></br>
        <Table width="100%" border="0" bordercolor="#000000" style="border=collapse" cellpading="3" cellspacing="0">
            <tr>
                <td>
                    <br></br>
                        <apex:outputText style="font-family:arial; font-size:14pt; font-weight:normal" value="Standard Terms and Conditions"></apex:outputText><br></br>
                    <br></br>
                </td>
            </tr>
            <tr>
                <td>
                    <br></br>
                        Rogers Standard Terms and Conditions Apply: http://rogerscorp.com/pages/termsconditions.aspx
                    <br></br>
                    <br></br>
                        No terms in addition to or that conflict with Rogers Standard Terms and Conditions that are contained in any document produced by Buyer shall be binding upon Rogers unless agreed to in a document bearing the manual signature of an authorized officer of Rogers.
                    <br></br>
                </td>
            </tr>
        </Table>
        <br></br>
        <Table width="100%" border="0" bordercolor="#000000" style="border=collapse" cellpading="3" cellspacing="0">
            <tr>
                <td>
                    <br></br>
                    <apex:outputText style="font-family:arial; font-size:14pt; font-weight:normal" value="Special Terms and Conditions"></apex:outputText><br></br>
                    <br></br>
                </td>
            </tr>
            <tr>
                <td>
                    <apex:outputText value="{!Quote.Special_Terms_and_Conditions__c}" escape="false"/>
                </td>
            </tr>
        </Table>         
</body>
        <footer>
        <Table width="100%" border="0" bordercolor="#000000" style="border=collapse" cellpading="3" cellspacing="0">
            <tr>
                <td>
                    <br></br>
                        <div align="center">
                        <img border="0" src="https://c.na31.content.force.com/servlet/servlet.ImageServer?id=0153700000063ge&oid=00D37000000PqpU&lastMod=1443643120000"/>
                        </div>
                    <br></br>
                </td>
            </tr>
        </Table>
        </footer>
</html>

</apex:pageBlock>
</apex:form>
</apex:page>
  • you are missing quotelineitem in your query. It might be in DSPQuoteTemplateAsPDF page controller so chat that. you need to add quotelineitem in soql – Himanshu Sep 26 '17 at 5:10
  • Quote Line Item is in the PDF Template – David Timpe Sep 26 '17 at 5:16
  • right. Please add your PDF template controller in question – Himanshu Sep 26 '17 at 5:29
  • Tried adding "QuoteLineItems" and "QuoteLineItems__r" to the SOQL without any success. – David Timpe Sep 26 '17 at 5:30
  • Did you modify/add the query in the Extenstion: DSPQuoteTemplateAsPDFExtension ? Also can you add the VF page code also for : DSPQuoteTemplateAsPDF .The problem seems to be with this page – Hemant Jain Sep 26 '17 at 6:02
0

So here are the three changes I would suggest.

  1. Add the extenstion in the VF page.

    '<'apex:page standardController="QUOTE" extensions = "DSPQuoteTemplateAsPDFExtension" showHeader="true" renderAs="PDF"'>'

  2. Change the constructor code as(Add QuoteLineItem fields as desired):

    public DSPQuoteTemplateAsPDFExtension(ApexPages.StandardController c) { q = (Quote)c.getRecord(); q= [SELECT Name, (SELECT Id,Product2.ProductCode FROM QuoteLineItems) FROM Quote WHERE ID = :q.Id]; }

  3. Change the way QuoteLineItems are reffered in VF page, i.e. from Quote.QuoteLineItems to q.QuoteLineItems.

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.