Finally: generate PDF documents in Batch ApexBefore, we lacked a friendly way to explain changes to customer accounts. We relied on exported Excel reports and complex statements with historical tracking, but neither was satisfactory to the customer. Batch PDF helped us increase customer satisfaction through added transparency. Now, we schedule the generation of simplified customer statements. Whenever questions come up, we easily pull up the "before" and "after" PDF copy of the statement and present to the customer a clear picture of what items had been added to their statement and when the addition occurred.
InstallationBatchPDF is easily installed from the AppExchange. 1. Get It Now from the Salesforce AppExchange
2. Click 'Install' to deploy the package
3. Follow the on-screen instructions
Tutorial1. Create your Visualforce Page, eg 'OpportunityDetail'<apex:page renderAs="pdf" standardController="Opportunity"> <apex:detail /> </apex:page>
2. Write your Batch Apex Class, eg 'OpportunityPrinter'public class OpportunityPrinter implements Database.Batchable<SObject>, Database.AllowsCallouts { public Database.QueryLocator start(Database.BatchableContext context) { return Database.getQueryLocator([SELECT Id, Name FROM Opportunity LIMIT 10]); } public void execute(Database.BatchableContext context, List<Opportunity> scopes) { Opportunity opportunity = scopes[0]; //use the relative url of your page BatchPDF.PageReference pr = new BatchPDF.PageReference('/apex/OpportunityDetail'); pr.getParameters().put('id', opportunity.Id); Blob data = pr.getContent(); //save the PDF as an attachment on the record insert new Attachment( Name = 'detail.pdf', Body = data, ParentId = opportunity.Id ); } public void finish(Database.BatchableContext context) { // } }
3. Execute your Batch Apex, eg from Developer ConsoleOpportunityPrinter batch = new OpportunityPrinter(); Database.executeBatch(batch, 1);
4. Verify your generated PDF outputIn this example, the Notes & Attachments related list now contains 'detail.pdf' showing your Opportunity: |