【作ってみた】採用アプリ編② -Apex共通クラスを作成-

本記事の内容

  • 共通して使用するApexクラスを作成

全体像

★採用アプリ範囲

★社員管理アプリ範囲

作成するクラス一覧

クラス名 用途
TriggerHandler.cls Apexトリガーが起動した際に呼ばれるクラス
Constants.cls 定数を定義するクラス
CommonMethod.cls 共通して使用できる処理のメソッドを定義するクラス

TriggerHandler.cls

public abstract class TriggerHandler {
    
    public TriggerHandler() {

    }

    protected virtual void onBeforeInsert() {}
    protected virtual void onBeforeUpdate() {}
    protected virtual void onBeforeDelete() {}
    protected virtual void onAfterInsert() {}
    protected virtual void onAfterUpdate() {}
    protected virtual void onAfterDelete() {}
    protected virtual void onAfterUndelete() {}

    public void run() {

        switch on Trigger.operationType {

            when BEFORE_INSERT {
                this.onBeforeInsert();
            }
            when BEFORE_UPDATE {
                this.onBeforeUpdate();
            }
            when BEFORE_DELETE {
                this.onBeforeDelete();
            }
            when AFTER_INSERT {
                this.onAfterInsert();
            }
            when AFTER_UPDATE {
                this.onAfterUpdate();
            }
            when AFTER_DELETE {
                this.onAfterDelete();
            }
            when AFTER_UNDELETE {
                this.onAfterUndelete();
            }
        }
    }
}

Constants.cls

  • よく使用する定数を定義しておく
public class Constants {

    /** 
     * トリガ種別 
     */
    public static final String TRIGGER_TIMING_BEFORE = 'before';
    public static final String TRIGGER_TIMING_AFTER  = 'after';
    public static final String TRIGGER_TYPE_INSERT   = 'insert';
    public static final String TRIGGER_TYPE_UPDATE   = 'update';
    public static final String TRIGGER_TYPE_DELETE   = 'delete';
    public static final String TRIGGER_TYPE_UNDELETE = 'undelete';

    /** 処理種別 */
    public static final String CUSTOMMETA_PROCESSCATEGORY_COPY   = 'コピー';
    public static final String CUSTOMMETA_PROCESSCATEGORY_INSERT = '作成';
    public static final String CUSTOMMETA_PROCESSCATEGORY_UPDATE = '更新';
    public static final String CUSTOMMETA_PROCESSCATEGORY_CHECK  = 'チェック';
    public static final String CUSTOMMETA_PROCESSCATEGORY_SUM    = '集計';
    public static final String CUSTOMMETA_PROCESSCATEGORY_SET    = 'セット';
    public static final String CUSTOMMETA_PROCESSCATEGORY_SERIAL = '連番';    
}

CommonMethod.cls

public class CommonMethod {
}

次回記事

作成中...