function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RelaxItsJustCodeRelaxItsJustCode 

Need help with using apex to select email template and attach PDF

I have extended the standard Contract object so it now has a master detail relationship with an object called Contract_Line_Item__c which calculates maint and displays what is maint is being renewed on.

 

I also have a VisualForce page that renders a great PDF now that is properly formatted to display the needed data.

 

THE PROBLEM:

 

I need your help to figure out the best way via apex to have this PDF attach itself to....

 

1.  The contract itself,

2. An HTML email template and send it automatically to the customer/contact listed on the contract,

3. Lastly, the ability to have the PDF print automatically for snail mail purposes....

 

I will give kudos to anyone that contributes.

 

Thank you,

Steve Laycock

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Yes, you'd create a new Visualforce page and just attach that to a button on the contract. As for emailing the attachment, you'll just need some URL hacking. I don't have the time to do the research on this, but try searching the forums. I'm sure someone has done something like that before.

All Answers

sfdcfoxsfdcfox

I've written all three of these items (our product also has contracts/contract line items...). These three tasks are a lot easier if you break them up into sections. I'll go over each in detail.

 

1. Two Visualforce pages. The first provides the logic necessary to save the PDF to the contract, the second contains the logic for the PDF itself. When the first page loads, it calls up the second page, uses getContent() to render the PDF, then saves it as an attachment to the contract.

 

2. Messaging.SingleEmailMessage, mostly, just set the template ID, add the attachment from the prior step to the email's file attachments, and send it off. You'll probably want a page backing this logic so the user has a chance to cancel/make sure it looks correct/etc, plus you can't render PDFs from some contexts, in case you need to basically invoke (1) as part of this process.

 

3. In theory, you can't "automatically print" from the web. The user has to confirm that they want to print. It's just a matter of security. However, you can semi-automatically print. In a Visualforce page, call "window.open" to open a new window with the URL for the PDF, then call "print()" on the reference to the new window, and then "close()" the new window. Most browsers will happily follow this chain of command to one-click print your PDF; the Print dialog will appear and then the user can choose to print.

RelaxItsJustCodeRelaxItsJustCode

sfdcfox,

 

This is my VF page as it is currently.

 

<apex:page standardController="Contract" showHeader="false" renderas="pdf">

<table border="0" cellspacing="0" cellpadding="0" width="100%" id="table1">
<tr>
    <td>
        <img src='{!URLFOR($Resource.APSLogoContract)}' title="logo" />
    </td>
    
    <td  align="center" style="font-size:20px;"><font face="Arial">
    <b>INVOICE</b></font>
    <table border="1" cellspacing="0" cellpadding="1" width="100%"  id="table77">
        <tr>
            <td bgcolor="#d7dee9">
                <font face="Arial"  style="font-size:13px;">Date: </font>
            </td>    
            <td>
                <font face="Arial" style="font-size:13px;">
                    <apex:outputText value="{0,date,MM'/'dd'/'yyyy}">
                        <apex:param value="{!contract.WF_Invoice_Date__c}" /> 
                    </apex:outputText>
                
                </font>
            </td>
        </tr>
        <tr>
            <td bgcolor="#d7dee9">
                <font face="Arial"  style="font-size:13px;">Due: </font>
            </td>    
            <td>
                <font face="Arial" style="font-size:13px;">Net 30</font>
            </td>
        </tr>    
    </table>     
    </td>
    

    
</tr>


</table>

<br/>

<table border="0" cellspacing="0" cellpadding="1" width="100%" id="table2">
<tr>
       <td colspan="2" style="font-size:13px;">
           <font face="Arial">BarbaraG@aps.us <br/><br/></font>
       </td>
</tr>
<tr>
       <td style="font-size:13px;">          
           <font face="Arial">Bill To:<br/><br/>
                             {!Contract.Account.Name}<br/>  
                             {!Contract.Account.BillingStreet}<br/>
                             {!Contract.Account.BillingCity} {!Contract.Account.BillingState} {!Contract.Account.BillingPostalCode} 
           </font>
        </td>
        
        <td width="20%"></td>
        
        <td style="font-size:13px;">
           <font face="Arial">Ship To:<br/><br/>
                              {!Contract.Account.Name}<br/>
                              {!Contract.Account.ShippingStreet}<br/>
                              {!Contract.Account.ShippingCity} {!Contract.Account.BillingState} {!Contract.Account.ShippingPostalCode}
           </font>
        </td>
</tr>    
</table>
<br/><br/><br/>


<table border="1" cellspacing="0" cellpadding="1" width="85%" id="table15">
<tr>
       <td bgcolor="#d7dee9" align="center"><font face="Arial">Contract Number</font></td>
       <td bgcolor="#d7dee9" align="center"><font face="Arial">Product</font></td>
       <td bgcolor="#d7dee9" align="center"><font face="Arial">Term</font></td>
       <td bgcolor="#d7dee9" align="center"><font face="Arial">Invoice Amount</font></td>
</tr>
<tr>
        <td align="center"><font face="Arial">
             {!Contract.ContractNumber}
            </font>
        </td>
        <td align="center"><font face="Arial">
             Maintenance
            </font>
        </td>
        <td align="center"><font face="Arial">
          <apex:outputText value="{0,date,MM'/'dd'/'yyyy}">
                <apex:param value="{!contract.startdate}" /> 
          </apex:outputText>-<apex:outputText value="{0,date,MM'/'dd'/'yyyy}">
              <apex:param value="{!Contract.Contract_End_Date__c}" />
          </apex:outputText>
            </font>
        </td>
        <td align="center"><font face="Arial">
            <apex:outputText value="{0,number,$#,###,###.##}">
    <apex:param value="{!Contract.Total_Maintenance_Price__c}" />
  </apex:outputText>

            </font>
        </td>
</tr>



</table>
<br/><br/>

<table border="1" style="border-color: #3B5E91" cellspacing="0" cellpadding="1" width="100%" id="table4">
<tr>
       <td bgcolor="#d7dee9" align="left"><font face="Arial">Product(s) Covered</font></td>
       <td bgcolor="#d7dee9" align="center"><font face="Arial">Quantity</font></td>      
</tr>
<tr>
       <apex:repeat value="{!Contract.Service_Contract_Line_Items__r}" var="line">
          <tr>
             <td align="left">{!line.Product_LU__r.name}</td>
             
             <td align="right">{!(ROUND(line.Quantity__c,0))}</td>
          </tr>
       </apex:repeat>  
</tr>
</table>
<br/><br/>

<table border="1" cellspacing="0" cellpadding="1" width="65%" id="table3">
<tr>
       <td bgcolor="#d7dee9" align="left"><font face="Arial">Please Note Our Remit To Address</font></td>
</tr>
<tr>
        <td><font face="Arial">
              Advanced Public Safety, Inc.<br/>
              PO Box 535208<br/>
              Atlanta, GA 30353-5208<br/><br/>
              Wire Instructions: Wells Fargo Bank<br/>
              ABA#: 121000248 Account #: 412-1360267

            </font>
        </td>

</tr>
</table>
<br/>


</apex:page>

 Can you give me more details as to how to attach it to the contract as well as attach it to the email?

 

Thank you,

Steve Laycock

 

sfdcfoxsfdcfox

To attach to the contract, you need a second page that loads the first (the one you posted). You can do it like this:

 

public PageReference saveAndEmail() {
    PageReference pdfPage = Page.contractPdf;
    pdfpage.getParameters().put('id', contractId);
    Attachment pdf = new Attachment(ParentId=contractId, ContentType='application/pdf', Name=contractName, Body=pdfPage.getContent());
    insert pdf;
    // return a link to the email page with parameters
}

Depending on if you're using the standard page or a custom page, your parameters will vary. I don't have time to go over the standard parameters right now, but the answer to that should already be on this forum.

RelaxItsJustCodeRelaxItsJustCode

sfdcfox,

 

It is a standard controller I'm using with this VF page PDF.

 

Sorry I'm a bit new to VF page management.

 

Can you please give me some more info?  (For example, the code snippet you have given me, where would it go? A class, another VF page, someplace else???)

 

Any additional info will be highly appriciated and kudos as always will be given.

 

Thank you,

Steve Laycock

sfdcfoxsfdcfox

Yes, you'd create a new Visualforce page and just attach that to a button on the contract. As for emailing the attachment, you'll just need some URL hacking. I don't have the time to do the research on this, but try searching the forums. I'm sure someone has done something like that before.

This was selected as the best answer
RelaxItsJustCodeRelaxItsJustCode

Thanks SFDCfox!

 

I will let you know how it turns out.

 

Steve