Thought i would share something which i needed to work on. I thank Jeff Douglas for his awesome post which helped me in this assignment.
Requirement
Requirement
There is a custom visualforce page which is rendered as pdf. There is a button on contact detail page which when clicked would call this vf page. These things were present. In addition to viewing this pdf from contact detail there was also a necessity to email these PDFs that too in bulk from list views.
Solution
list button was created with source as VF page.
<apex:page standardController="contact" recordSetVar="Props" extensions="emailcontact" action="{!sendingemail}">
</apex:page>
</apex:page>
Controller is where all the code is
public class emailcontact {
public ApexPages.StandardSetController propcontroller{get;set;}
public list<contact> props{get;set;}
public emailcontract(ApexPages.StandardSetController controller) {
propcontroller =(ApexPages.StandardSetController)controller;
}
public pagereference sendingemail(){
props = propcontroller.getSelected();// Contains the list of record which is selected from the list
PageReference pdf = Page.VFpagetoberenderedaspdf;
for(contact pr : props)
{
pdf.getParameters().put('id',pr.Id);
attachment attach = new attachment();
Blob body;
try {
body = pdf.getContentAsPDF();
}
catch (VisualforceException e) {
body = Blob.valueOf('Missing Text');
}
// ********************* for attachment to the contactrecord ******************************
try{
attach.Body = body;
attach.Name = 'Contact' +system.today+'.pdf';
attach.IsPrivate = false;
attach.ParentId = pr.Id;
att.add(attach);
}
catch(exception e)
{
}
// ************************ for email creation************************************
Messaging.EmailFileAttachment attach2 = new Messaging.EmailFileAttachment();
attach2.setContentType('application/pdf');
attach2.setFileName('Contact.pdf');
attach2.setInline(false);
attach2.Body = body;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {pr.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Offer for' + pr.name);// alternatively use templates as well
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach2 });
mail.setHtmlBody('Please find attached a cash offer for your listing at ');
em_sent.add(mail);
}
}
if(att!= null)
insert att;
if(em_sent != null)
Messaging.sendEmail(em_sent);
pagereference pr = new pagereference('/a0e/o');//will redirect to contact tab
return pr;
}
}
public ApexPages.StandardSetController propcontroller{get;set;}
public list<contact> props{get;set;}
public emailcontract(ApexPages.StandardSetController controller) {
propcontroller =(ApexPages.StandardSetController)controller;
}
public pagereference sendingemail(){
props = propcontroller.getSelected();// Contains the list of record which is selected from the list
PageReference pdf = Page.VFpagetoberenderedaspdf;
for(contact pr : props)
{
pdf.getParameters().put('id',pr.Id);
attachment attach = new attachment();
Blob body;
try {
body = pdf.getContentAsPDF();
}
catch (VisualforceException e) {
body = Blob.valueOf('Missing Text');
}
// ********************* for attachment to the contactrecord ******************************
try{
attach.Body = body;
attach.Name = 'Contact' +system.today+'.pdf';
attach.IsPrivate = false;
attach.ParentId = pr.Id;
att.add(attach);
}
catch(exception e)
{
}
// ************************ for email creation************************************
Messaging.EmailFileAttachment attach2 = new Messaging.EmailFileAttachment();
attach2.setContentType('application/pdf');
attach2.setFileName('Contact.pdf');
attach2.setInline(false);
attach2.Body = body;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {pr.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Offer for' + pr.name);// alternatively use templates as well
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach2 });
mail.setHtmlBody('Please find attached a cash offer for your listing at ');
em_sent.add(mail);
}
}
if(att!= null)
insert att;
if(em_sent != null)
Messaging.sendEmail(em_sent);
pagereference pr = new pagereference('/a0e/o');//will redirect to contact tab
return pr;
}
}
Also you can add a piece of code to send an email to yourself to indicate the success or failure of the action or display it in the page
Limitations
Major limitation i faced is no of the records the code can process (Governor limits on callouts). It was limited to 10 after which time out error happened. I tried using batch as well as future calls. Both returned me empty PDFs. Waiting for Salesforce to remove these limitations in future.