1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Salesforce]EventLogFileをファイルに保存するPGM

Last updated at Posted at 2022-02-27

日次バッチ処理でEventLogFileを取得してSalesforce上のファイルに保存するPGM.
お作法がなってないかもしれませんが大目に見て欲しいです.
誰かの参考になれば幸いです.

PGM

DownloadEventLogFileBatch
public class DownloadEventLogFileBatch implements Database.Batchable<sObject>, Database.Stateful{
    // *************************************************
    // EventLogFileをダウンロードしてファイルに格納する。
    //
    // Date targetLogDate = Date.newInstance(2022,2,24);
    // DownloadEventLogFileBatch bat = new DownloadEventLogFileBatch(targetLogDate);
    // Id jobId = Database.executeBatch(bat);
    //
    // *************************************************

    private Date targetLogDate;

    /**
     * コンストラクタ
     * @param なし
     * @return void
     */
    public downloadEventLogFileBatch() {
    }

    /**
     * コンストラクタ
     * @param targetLogDate Date 取得したいログの日付(GMTで)
     * @return void
     */
    public downloadEventLogFileBatch(Date targetLogDate) {
        this.targetLogDate = targetLogDate;
    }

    /**
     * start処理
     * @param BC Database.BatchableContext
     * @return Database.QueryLocator
     */
    public Database.QueryLocator start(Database.BatchableContext BC){
        // クエリ生成
        Time t = Time.newInstance(0,0,0,0);
        Datetime dt;
        if(targetLogDate == null){
            dt = DateTime.newInstanceGMT(DateTime.now().dateGMT(),t).addDays(-2);
        } else {
            dt = DateTime.newInstanceGMT(targetLogDate,t);
        }
        System.debug('dt:'+dt);
        String query = 'SELECT id,EventType,LogDate,LogFile FROM EventLogFile WHERE LogDate = ' + dt.format('yyyy-MM-dd\'T\'00:00:00.000\'+0000\'');
        System.debug('query:'+query);
        return Database.getQueryLocator(query);
    }

    /**
     * execute処理
     * @param BC Database.BatchableContext
     * @param scope List<sObject>
     * @return void
     */
    public void execute(Database.BatchableContext BC, List<sObject> scope){
        try{
            List<ContentVersion> cvList = new List<ContentVersion>();
            for(EventLogFile elf : (List<EventLogFile>) scope){
                ContentVersion cv = new ContentVersion();
                cv.ContentLocation = 'S'; // ファイルはSalesforce上に保存
                cv.PathOnClient = String.valueof(elf.LogDate.dateGMT())+'_'+elf.EventType+'.csv'; // ファイル名
                cv.Title = String.valueof(elf.LogDate.dateGMT())+'_'+elf.EventType+'.csv'; // ファイルタイトル
                cv.VersionData = elf.LogFile; // ファイルデータ
                cvList.add(cv);
            }
            if(cvList.size() > 0){
                insert cvList;
            }
        } catch(Exception e) {
            System.debug('Exception:'+e.getMessage());
        }
    }

    /**
     * finish処理
     * @param BC Database.BatchableContext
     * @return void
     */
    public void finish(Database.BatchableContext BC){    
    }
}
DownloadEventLogFileBatchSchedule
global class DownloadEventLogFileBatchSchedule implements Schedulable {
    
    public downloadEventLogFileBatchSchedule() {
    }

    /**
    * バッチ処理の起動
    * @param SC SchedulableContext
    * @return void 
    */
    global void execute(SchedulableContext SC){
        Id batchInstanceId = Database.executeBatch(new DownloadEventLogFileBatch());
    }
}

スケジュール済みジョブ

スケジュール例
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?