自動採番仕組み

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/332907/fbea4a01-df53-09b9-5db8-ba6499568ff5.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/332907/3aa0dbb3-0757-fc40-b284-2f39f76585b5.png)


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/332907/36c2b388-4ef6-a1d6-3b53-7fcfdcf25e29.png)

```java:AutoNumberUtil.cls
public without sharing class AutoNumberUtil {
    public static String KUBUN01 = '1001';
    public static List<String> executeBulk(String kubun, Integer size) {
        List<String> newValueList = new List<String>();
        AutoNumberMaster__c currentAutoNumberMaster = getCurrentAutoNumberMaster(kubun);
        String oldValue = currentAutoNumberMaster.Value__c;
        String newValue;
        for (Integer index=0; index < size; index++) {
            switch on kubun {
                when '1001' { newValue = bizLogicKubun01(oldValue); }
            }
            newValueList.add(newValue);
            oldValue = newValue;
        }
        AutoNumberMaster__c autoNumberMaster = new AutoNumberMaster__c();
        autoNumberMaster.Code__c = kubun;
        autoNumberMaster.Value__c = newValue;
        upsert autoNumberMaster Code__c;
        return newValueList;
    }
    private static String bizLogicKubun01(String oldValue) {
        String template = '{0}-{1}';
        List<Object> parameters = new List<Object>();
        String parameter0 = System.now().format('yyyyMMdd');
        parameters.add(parameter0);
        String parameter1 = '0001';
        if (String.isNotBlank(oldValue)) {
            List<String> autoNumberParts = oldValue.split('-');
            if (parameter0.equals(autoNumberParts.get(0))) {
                Long autoNumberSuffix = long.valueOf(autoNumberParts.get(1)) + 1;
                if (autoNumberSuffix <= 999) {
                    parameter1 = ('0000' + String.valueOf(autoNumberSuffix)).right(4);
                } else {
                    parameter1 = String.valueOf(autoNumberSuffix);
                }
            }
        }
        parameters.add(parameter1);
        return String.format(template, parameters);
    }
    private static AutoNumberMaster__c getCurrentAutoNumberMaster(String kubun) {
        List<AutoNumberMaster__c> autoNumberMasterList = [SELECT Code__c, Value__c FROM AutoNumberMaster__c WHERE Code__c = :kubun FOR UPDATE];
        if (autoNumberMasterList.isEmpty()) {
            return new AutoNumberMaster__c();
        }
        return autoNumberMasterList.get(0);
    }
}
```

```java:AutoNumberTestTriggerHandler.cls
public with sharing class AutoNumberTestTriggerHandler extends TriggerHandler {
    /**
     * @description before insert トリガーのハンドラ処理。
     * @param newList 対象データ。
     */
    public override void beforeInsert(List<SObject> newList) {
        // 対象データを取得する
        List<AutoNumberTest__c> autoNumberTestList = (List<AutoNumberTest__c>)newList;
        
        // Bulk採番処理
        List<String> controlNumberList = AutoNumberUtil.executeBulk(AutoNumberUtil.KUBUN01, newList.size());
        
        // 件数分により、採番番号を設定する
        for (Integer index=0; index<newList.size(); index++) {
            AutoNumberTest__c item = autoNumberTestList .get(index);
            item.AutoNumber__c = controlNumberList.get(index);
        }
    }
}
```

```java:AutoNumberTestTrigger.trigger
trigger AutoNumberTestTrigger on AutoNumberTest__c (before insert) {
    new AutoNumberTestTriggerHandler().execute();
}
```