【Salesforce】バッチ処理でPDFファイルを保存する
Sallesforceから請求書を発行したい場合、月末処理で作成してSalesforce上に保存したい場合があります。
Apexのバッチ処理によって月末にまとめてPDFを作成する方法をメモします。
事前に保存するPDF帳票を作成する必要があります。
テスト用に取引先の情報を表示するだけのPDF用ページを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | < apex:page id = "Page" standardController = "Account" standardStylesheets = "false" showHeader = "false" sidebar = "false" applyHtmlTag = "false" > < head > < style > @page { size: 8.27in 11.69in; padding: 0; } body { font-family: Arial Unicode MS; font-size: 10pt; text-align: left; } </ style > </ head > < body > < apex:form id = "Form" > < apex:pageblock > < apex:pageBlockSection title = "取引先情報" columns = "2" > < apex:pageBlockSectionItem > < apex:outputlabel for = "Name" value = "取引先名" /> < apex:outputfield id = "Name" value = "{!Account.Name}" /> </ apex:pageBlockSectionItem > < apex:pageBlockSectionItem ></ apex:pageBlockSectionItem > < apex:pageBlockSectionItem > < apex:outputlabel for = "Fax" value = "fax" /> < apex:outputfield id = "Fax" value = "{!Account.Fax}" /> </ apex:pageBlockSectionItem > < apex:pageBlockSectionItem > < apex:outputlabel for = "Phone" value = "電話番号" /> < apex:outputfield id = "Phone" value = "{!Account.Phone}" /> </ apex:pageBlockSectionItem > </ apex:pageBlockSection > </ apex:pageblock > </ apex:form > </ body > </ apex:page > |
今回はコントローラのクラスを指定していませんが、もちろん指定したページでも大丈夫です。
PDFにするため、PDFで日本語が表示できる形式のページにする必要があります。
バッチ処理クラスです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | /** * 取引先を表示したページをPDFにするバッチ */ global class CreatePDFBatch implements Database.Batchable<sObject> { /** * 前処理 * 取引先レコードを取得する */ global Database.QueryLocator start(Database.BatchableContext BC){ String query; try { // カスタム設定取得用 query = ' ' ; query += ' select ' ; query += ' Id, ' ; query += ' Name' ; query += ' from ' ; query += ' Account ' ; } catch (Exception e){ system.debug( '【エラー】' + e.getMessage()); system.debug( '【エラー】' + e.getStackTraceString()); } return Database.getQueryLocator(query); } /** * 実処理 * 取引先を元にPDFを作成する */ global void execute(Database.BatchableContext BC, List<Account> accountList){ List<Attachment> attachmentList; String fileNameTemplate; try { // ファイル名のテンプレートを作成する fileNameTemplate = '_' + Date.today().Month() + '月.pdf' ; // 添付ファイルリストを作成する attachmentList = new List<Attachment>(); // 取得した取引先のIDからPDFを作成する for (Account acc : accountList){ Attachment tmpAttachment; PageReference pr; // PDFにするページを作成する pr = Page.AccoutView; pr.getParameters().put( 'id' , acc.Id); // 添付ファイルを作成する tmpAttachment = new Attachment(); tmpAttachment.Name = acc.Name + fileNameTemplate; tmpAttachment.ParentId = acc.Id; tmpAttachment.Body = pr.getContentAsPDF(); // リストに追加する attachmentList.add(tmpAttachment); } // 添付ファイルを保存する insert attachmentList; } catch (Exception e){ system.debug( '【エラー】' + e.getMessage()); system.debug( '【エラー】' + e.getStackTraceString()); } } /** * 終了処理 * 特に何もしない */ global void finish(Database.BatchableContext BC){} } |
すべての取引先を取得しています。
Page.AccoutViewで上で作成したVisualforceのページを取得し、取引先のIDを指定しています。
作成したPageReferenceのインスタンスの「getContentAsPDF」メソッドを使用することで、PDFファイルのBodyを取得できます。
https://developer.salesforce.com/docs/atlas.ja-jp.pages.meta/pages/apex_System_PageReference_methods.htm
添付ファイルの親を取引先に指定してInsertしています。
一応、スケジューラのクラスも記述します。
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * 取引先の情報を表示したPDFを作成するバッチのスケジューラ */ global class CreatePDFBatchScheduler implements Schedulable { global void execute(SchedulableContext sc) { CreatePDFBatch batch = new CreatePDFBatch(); // バッチ呼び出し database.executebatch(batch, 200 ); } } |
バッチサイズを200に指定していますが、PDFファイルにする対象のページが大きい場合はエラーが出る可能性があります。
エラーが出る場合はバッチサイズを下げて処理を行いましょう。
既に帳票用のVisualforceページが存在する場合はバッチ処理を作成するだけなので、すぐに作ることが出来そうですね。
ちなみに、作成したPDFファイルはドキュメントに保存することもできるので、そのあたりはお好みです。
今回は分かりやすいように取引先の添付ファイルに設定しました。
No comments.