How to generate PDF invoice and send on email using web to lead form in salesforce

Sept 23, 2023


 

 

After completing this unit, you will able to:

 

Introduction

Efficiently managing invoices is a crucial aspect of any business, and Salesforce is a powerful platform for streamlining various aspects of sales and customer relationship management. One common requirement for businesses is the ability to send invoices to leads or customers as PDFs directly from Salesforce. In this article, we'll walk you through the process of sending invoices as PDFs using Web-to-Lead forms in Salesforce, providing you with a convenient way to automate and simplify your invoicing process.

 

Set Up Web-To-Lead Form in Salesforce

Before you can send invoices as PDFs, you'll need to set up Web-to-Lead forms in Salesforce. These forms allow you to capture lead information directly from your website and automatically create lead records in Salesforce. Follow these steps to set up Web-to-Lead forms:

  1. Log in to your Salesforce account
  2. In the Quick Find box, type "Web-to-Lead" and select "Web-to-Lead."
  3. Click the "Create Web-to-Lead Form" button.
  4. Configure the form fields you want to include, such as name, email, phone number, and any custom fields for invoice details.
  5. Click "Generate" to generate the HTML code for your form.
  6. Copy the HTML code and paste it onto your website where you want the form to appear.
  7. Save your Web-to-Lead form.

 

Customize the Thank You Page:

 

Create a PDF Invoice Using Visualforce

Next, you'll need to create an invoice template that you can use to generate PDF invoices.

We will create an invoice template using the visualforce page.

 

The following is the visualforce code:

InovicePdf.vfp

<apex:page standardController="Contact" renderAs="pdf" >

    <h1>Invoice</h1>

    <div class="slds-card" style="border: 1px solid black;">

    <div  class="slds-card" style="padding-top:23px; padding-left:15px; padding-right:30px; padding-bottom:10px;">

        

        <div>            

            <table style="border-collapse: collapse;border: 1px solid #5e73a2; font-family:sans-serif; width:100%;color: #13203a; font-size: 13px;">

                <tr>

                    <td style="vertical-align:top;">

                         Your Logo                    

                    </td>

                    <td align="right" style="vertical-align:top;"><span style="font-weight:bold; color:#374aac;padding:10px;">Invoice</span><br/><span style="padding:10px;"></span> <br/><br/><span style="font-weight:bold; color:#374aac; padding:10px;">Service Date:</span><br/><span style="padding:10px;"></span></td>

                 </tr>

            </table>

        </div>

            <hr class="solid" style="border-top: 1px solid #bbb;"/>

        <div>

            <table style="border-collapse: collapse;border: 1px solid #5e73a2; font-family:sans-serif; width:100%;color: #13203a; font-size: 13px;">

                <tr>

                    <th style="font-weight:bold; color:#374aac; padding:10px;width:40%; padding-left:25px;">Invoice For</th>

                    <th align="right" style="vertical-align:top;"><span style="font-weight:bold; color:#374aac;padding:10px; ">Payable to</span><br/><span style="font-family:Arial, sans-serif; color:gray; font-size:13px;padding:10px;">Your Company Name</span></th>

                </tr>

                <tr>

                    <td style="font-family:Arial, sans-serif; color:gray; font-size:13px; padding-left:25px;"><br/><br/></td>

                    <th align="right" style="vertical-align:bottom;"><span style="font-weight:bold; color:#374aac;padding:10px;">Invoice</span><br/><span style="font-family:Arial, sans-serif; color:gray; font-size:13px;padding:10px;">Industry</span></th>

                </tr>

                <tr>

                    <td></td>

                    <td align="right" style="font-family:Arial, sans-serif; color:gray; font-size:13px; padding-left:20px; padding:10px"></td>

                </tr>

            </table>

        </div>

        <hr class="solid" style="border-top: 1px solid #bbb;"/>

        <div>

            <table style="border-collapse: collapse;border: 1px solid #5e73a2; font-family:sans-serif; width:100%;color: #13203a; font-size: 13px;">

                <thead>

                <tr>

                    <th style="font-weight:bold; color:#374aac; padding:10px;padding-left:25px;width:80%">Job Description</th>

                    <th align="right" style="padding:10px;color:#374aac; width:30%">Total price</th>

                </tr>

                </thead>

                <tbody>

                    <apex:variable var="count" value="{!0}"/>

                    <apex:variable var="sumTotal" value="{!0}"/>

                    <!--apex:repeat value="" var="obj">

                         <tr style="{!IF(MOD(count,2)==0, 'background-color: #FFFFFF;','background-color: #E5E7E9;')}">

                            <td role="gridcell" style="height:5px; font-family:Arial, sans-serif; color:gray; font-size:13px; padding-left:25px;">

                                <apex:outputText>

                                </apex:outputText>

                            </td>

                            <td align="right" role="gridcell" style="height:5px; font-family:Arial, sans-serif; color:gray; font-size:13px; padding-left:20px; padding:10px">

                                <span></span>

                            </td>

                         </tr>

                        <apex:variable var="count" value="{!count+1}"/>

                        <apex:variable var="sumTotal" value=""/>

                    </apex:repeat-->

                    <tr style="height:60px">

                        <td role="gridcell" style="vertical-align:bottom; height:5px; font-family:Arial, sans-serif; color:gray; font-size:13px; padding-left:25px;">

                                <span>GRAND TOTAL</span>                                

                        </td>

                        <td align="right" role="gridcell" style="vertical-align:bottom; height:5px; font-family:Arial, sans-serif; font-size:13px; padding-left:20px; padding:10px">

                                <span></span>

                        </td>

                    </tr>

                    <tr style="height:30px">

                        <td role="gridcell" style=" vertical-align:bottom;height:5px; font-family:Arial, sans-serif; color:gray; font-size:13px; padding-left:25px;">

                                <span>Warranty: </span>                                

                        </td>

                        <td></td>

                    </tr>

                </tbody>

            </table>

        </div>

        <hr class="solid" style="border-top: 1px solid black; margin-top:25px"/>

        

        <div>

            <table style="border-collapse: collapse;border: 1px solid #5e73a2; font-family:sans-serif; width:100%;color: #13203a; font-size: 13px;">

                    <tr style="height:20px">

                        <td role="gridcell" style="height:5px; font-family:Arial, sans-serif; font-weight:bold;font-size:13px; padding-left:25px;">

                                <span>Notes:</span>                                

                        </td>

                        <td align="right" role="gridcell" style="vertical-align:bottom; height:5px; font-family:Arial, sans-serif; font-size:13px; padding-left:20px; padding:10px">

                                <span>Subtotal</span>

                        </td>

                        <td align="right" role="gridcell" style="vertical-align:bottom; height:5px; font-family:Arial, sans-serif; font-size:13px; padding-left:20px; padding:10px">

                                <span></span>

                        </td>

                    </tr>

                    <tr style="height:30px">

                        <td role="gridcell" style="vertical-align:top;height:5px; font-family:Arial, sans-serif; font-size:13px; padding-left:25px;">

                                <div style="border: 1px solid black; padding:10px;">Thank you for your business!</div>                                

                        </td>

                        <td align="right" role="gridcell" style="vertical-align:top; height:5px; font-family:Arial, sans-serif; font-size:13px; padding-left:20px; padding:10px">

                                <span>Other</span>

                        </td>

                        <td align="right" role="gridcell" style="font-weight:bold;color:#374aac; vertical-align:bottom; height:5px; font-family:Arial, sans-serif; font-size:20px; padding-left:20px; padding:10px">

                                <span></span>

                        </td>

                    </tr>

            </table>

        </div>

    </div>

        </div>

</apex:page>

 

 

 

Output of the above visualforce code will look like this:

 

 

Send the Invoice via Email:

When we fill data into web to lead form and submit the form, the trigger will invoke and from trigger we can call apex class to send email.

To send an invoice via email using Apex code in Salesforce, you can use Salesforce's built-in Messaging.SingleEmailMessage class and the Messaging.sendEmail method.

 

LeadTrigger.apxt
Trigger LeadConvertTrigger on Lead (after insert) {        
            List<Contact> conRec = new List<Contact>();    
            for(Lead leadRec : Trigger.new)
            {      
                if(leadRec.Email != '' || leadRec.Phone !='' || leadRec.MobilePhone !='')
                {
                      InvoicePdfController.sendEmailWithAttachment(leadRec.Id, leadRec.Email);
                }    
            }
}

 

InvoicePdfController.apxc
@future(callout=true)
public class InvoicePdfController {
   
    public static void sendEmailWithAttachment(String toAddress)
    {
        String requestURL = '/apex/InovicePdf;        
        Blob pdfData = new PageReference(requestURL).getContent();
       
        ContentVersion cv = new ContentVersion();
        cv.VersionData = pdfData;
        cv.Title = 'Invoice '+ Date.today().format().replace('/', '-');
        cv.PathOnClient = cv.Title + '.pdf';
        insert cv;
       
        List<String> contentVersionIds = new List<String>();
        contentVersionIds.add(cv.Id);
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        string[] to = new string[] {toAddress};
        message.setToAddresses(to);        
        message.setTreatTargetObjectAsRecipient(false);        
        message.setSubject('Send Invoice PDF');        
        message.setEntityAttachments(contentVersionIds);
        message.setHtmlBody('Html body Content');          
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { message });          
    }
}

 

 

Conclusion

In conclusion, sending an invoice as a PDF using a web-to-lead form in Salesforce involves a series of steps to create, store, and deliver the invoice to your leads. It's a process that combines web form customization, document generation, email communication, and potentially automation through Apex triggers.

Ensure that you carefully design and test the entire process to provide a seamless experience for your customers while complying with any legal and regulatory requirements related to invoicing and data handling. By following these steps and continuously monitoring and improving the process, you can efficiently send invoices and enhance your customer engagement within the Salesforce ecosystem.

 

 

I hope this blog helped you!