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
PamSalesforcePamSalesforce 

Using custom button to generate a pdf and send it as an attachment in email

Hi,

 

I need to add a custom button on a detail field that create a pdf document using the custom object data

and send a mail using a mail template with the created pdf document in attachement.

 

Can anyone suggest me how to implement this.

 

Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
PamSalesforcePamSalesforce

Hi,

 

I had faced this issue but it is solved now.

 

This is how i implemented it:

 

I created a custom button with javascript code on it to call the webservice method. This web sercvice method in gets the visualforce page which is rendered as PDF, attaches this pdf to the outbound email and sends the email.

 

Here is the code:

code on custom button:

{!requireScript("/soap/ajax/16.0/connection.js")}
{!requireScript("/soap/ajax/16.0/apex.js")}
var idLead = '{!Lead.Id}';
sforce.apex.execute("sendPDFEmailClass","emailPdf",{a:idLead });

 

the sendPDFEmailClass

global class sendPDFEmailClass
{
/* WebService to send email with pdf attachment.
This method is called on click of Custom Button Email Pdf/
*/
WebService static void emailLeadPdf(ID leadId)
{

PageReference pdfPage= Page.sendPdfEmail;
pdfPage.getParameters().put('id',leadId);

Blob pdf1 = pdfPage.getContent();
...
Messaging.Singleemailmessage mail= new Messaging.Singleemailmessage();
....
// Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
// neat - set name of PDF
efa.setFileName(lead[0].Name+ '.pdf');
// set email body
efa.setBody(pdf1 );
//attach the PDF
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// send it, ignoring any errors (bad!)
system.debug('Sending email');
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

}
 

  and the visualforce page should have  renderAs="pdf" in <apex:page>..

 

This worked for me.

 

Hope this helps those who are facing problems similar to this.

All Answers

PamSalesforcePamSalesforce

Hi,

 

I had faced this issue but it is solved now.

 

This is how i implemented it:

 

I created a custom button with javascript code on it to call the webservice method. This web sercvice method in gets the visualforce page which is rendered as PDF, attaches this pdf to the outbound email and sends the email.

 

Here is the code:

code on custom button:

{!requireScript("/soap/ajax/16.0/connection.js")}
{!requireScript("/soap/ajax/16.0/apex.js")}
var idLead = '{!Lead.Id}';
sforce.apex.execute("sendPDFEmailClass","emailPdf",{a:idLead });

 

the sendPDFEmailClass

global class sendPDFEmailClass
{
/* WebService to send email with pdf attachment.
This method is called on click of Custom Button Email Pdf/
*/
WebService static void emailLeadPdf(ID leadId)
{

PageReference pdfPage= Page.sendPdfEmail;
pdfPage.getParameters().put('id',leadId);

Blob pdf1 = pdfPage.getContent();
...
Messaging.Singleemailmessage mail= new Messaging.Singleemailmessage();
....
// Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
// neat - set name of PDF
efa.setFileName(lead[0].Name+ '.pdf');
// set email body
efa.setBody(pdf1 );
//attach the PDF
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// send it, ignoring any errors (bad!)
system.debug('Sending email');
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

}
 

  and the visualforce page should have  renderAs="pdf" in <apex:page>..

 

This worked for me.

 

Hope this helps those who are facing problems similar to this.

This was selected as the best answer
Rick-ProfaceRick-Proface
I can't seem to get this to work. Is there more to it?
VK86VK86

I m facing the same issue here.

 

Any further help with an example is much appreciated.

 

Thanks,

VK 

PamSalesforcePamSalesforce
Can you post your code or be more specific about the issue you are facing.
VK86VK86

Hello Pam,

 

I tried to implement the code which you provided by changing the variables for this issue here 

I am a newbie to the apex programming thing and recently started learning more about  this.

 

Thanks for your quick reply and any help is much appreciated.

 

Thanks,

Vinay 

PamSalesforcePamSalesforce

Here is a blog on emailing VF as attachment. Hope this helps.

http://blog.sforce.com/sforce/2008/06/emailing-visual.html

VK86VK86

Thanks a lot Pam... 

Marypearl ParnellMarypearl Parnell
Is it possible to have a button created that ONLY generates a PDF on my custom object?
sanjay pegasanjay pega
Hello everyone please tell me Is it possible to convert the newly created Account into PDF and send it through mail 
Thanks in advance