Skip to main content

複数レコードをプリンターから印刷する

商談オブジェクトの複数のレコードのデータを、Apex 一括処理ジョブによってバッチ単位で印刷を実行します。

参考

Apex 一括処理ジョブからプロシージャーを呼び出す場合、レコード IDのリストをプロシージャー呼び出しのパラメーターとして指定する実装が考えられますが、SOQLにレコード IDのリストを含めるとSOQL全体が長くなり、Salesforceの制限によりエラーが発生します。

Apex 一括処理ジョブでは、Database.executeBatch メソッドのscopeパラメーターにより、バッチごとの最大レコード数が指定できるため、scopeパラメーターを調整の上、プロシージャー実行時にSOQLエラーにならないように実装してください。最大レコード数の目安は、最大で500程度です。

バッチ処理で分割された場合、プロシージャが複数回呼び出され、印刷データもバッチ単位での処理になります。

500件以上のレコードを、1回のプロシージャー呼び出しで処理したい場合は、「複数レコードをinvoiceAgent 文書管理にアーカイブする」のように、プロシージャーのクエリー条件にて対象レコードを絞り込む方法を検討してください。

ボタンとプロシージャーの設定のポイント

  • プリンターから印刷するために、次の設定をします。

    • ボタンの作成時

      icon_menu_Button.png[SVFボタン設定]画面-[SVFボタン情報]-icon_setting.png-[アクション]タブで、icon_directPrint.pngを有効化

    • プロシージャーの作成時

      icon_menu_procedure.png[プロシージャー]画面-[アクション]で、「ダイレクトプリント」を選択

  • レコードごとにページを区切るために、次の設定をします。

    • ボタンの作成時

      icon_menu_Button.png[SVFボタン設定]画面-[SVFボタン情報]-icon_setting.png-[ファイル]タブ-[帳票レイアウトの切り替え]で、[レコード単位で切り替える]を有効化

  • パラメーターを設定します。

    • パラメーターの設定時

      名前

      既定値

      クォーテーション

      説明

      limit

      1

      なし

      Apex クラス「SvfCloudDirectPrintBatch」によって、値が指定されます。

      id

      (設定なし)

      なし

      Apex クラス「SvfCloudDirectPrintBatch」によって、カンマ区切りで連結したレコード IDが指定されます。

  • レコードの抽出条件を、メインクエリー条件で設定します。

    • クエリー条件の設定時

      フィルター

      ソート

      リミット

      オフセット

      メインクエリー条件

      id in ( ${id} ) *1

      (設定なし)

      ${limit}

      ${offset}

      *1 カンマ区切りで連結したレコード IDを、IN 句に指定します。

Apex クラスのサンプル

illust_procedure_DirectPrint.png
  • executeBatch

    Apex クラス「SvfCloudDirectPrintBatch」を呼び出しを、Apex 一括処理ジョブに追加します。対象のレコードを500件ずつ処理するように指定しています。

  • SvfCloudDirectPrintBatch

    SVF Cloud Managerで作成したプロシージャー「OpportunityProcedure_DirectPrint」を呼び出します。

  • SvfCloudProcedure(共通のApex クラス)

    すべてのサンプルが使用する、共通のApex クラスです。

executeBatch

// Apex 一括処理にて、バッチ単位でプロシージャー呼び出しを非同期に実行します。
SvfCloudProcedure  proc = new SvfCloudProcedure (
    'a0xxxXXX',                        // テナントID
    'svfCloudCertName',                // 「証明書と鍵の管理」に登録した証明書の名前
    UserInfo.getUserName(),            // SVF CloudからSalesforce RESTを実行するSalesforceユーザ名
    'OpportunityProcedure_DirectPrint' // SVF Cloud Managerで定義したプロシージャーの名前
);
Database.executeBatch(
    new SvfCloudDirectPrintBatch  (
        proc,
        'SELECT Id FROM Opportunity WHERE CloseDate = THIS_MONTH',
                                       // 今月が完了予定日のレコード
        'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
                                       // 出力したいプリンターID
    )
    ,500                               // *1
);

*1 Database.executeBatch メソッドのscopeパラメーターは、最大で500程度としてください。大きすぎると、SVF CloudからSOQLを実行する際、エラーとなる場合があります。

SvfCloudDirectPrintBatch

global with sharing class SvfCloudDirectPrintBatch implements Database.Batchable< sObject >, Database.AllowsCallouts {
    private SvfCloudProcedure proc;
    private String query;
    private String printerId;
    
    global SvfCloudDirectPrintBatch(SvfCloudProcedure proc, String query, String printerId){
        
        this.query=query;
        this.proc = proc;
        this.printerId = printerId;
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List< sObject > scope){
        
        // プロシージャーのパラメーター"id"に設定するレコード IDのリストを作成します。
        List<String> idList = new List<String>{};
            for(sObject s : scope){
                idList.add(s.id);
            }
            
        // プリンターIDを指定します。
        Map<String,String> params = null;
        if ( printerId != null ){
            params = new Map<String,String>{
             'printer' => this.printerId
            };
        }
        
        try {
            // プロシージャーを実行します。
            String actionId = proc.executeProcedure(idList,params);
            
            // 処理状況を確認します。            
            proc.waitStatus(60);
        }
        catch (SvfCloudProcedure.SvfCloudException e){
            System.debug(e);
            throw e;
        }
        catch (CalloutException e){
            System.debug(e);
            throw e;
        }
    }
    
    global void finish(Database.BatchableContext BC){
    }
}