0
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 3 years have passed since last update.

Apexカスタイマイズ可能な自動採番

0
Last updated at Posted at 2023-01-23

同時更新されない頭文字と長さを自由に設定する自動採番する数字を一箇所に表示する方法をこの記事で教えます。

1.専用のオブジェクト

表示するオブジェクトOrder_Headerのレコードをテキスト型に設定する
image.png
image.png
採番を管理するオブジェクトSequenceNoを作成する
image.png

2.メソッド作成する

頭文字を決めるprefix + 数字を決めるSequeceId + 尾語決めるsuffix で”Case00000x”の形式にする。

 public static String getNewOrderHeaderCaseNo(){
        //更新が被らないようにFOR UPDATEを使い排他性をつける
        SequenceNo__c lists0 = [SELECT id,Type__c,prefix__c,sequenceId__c,suffix__c From SequenceNo__c WHERE Type__c='CaseNo' Limit 1 FOR UPDATE];
        
        lists0.sequenceId__c+=1;
        Decimal maxCaseNo=lists0.sequenceId__c;
        String newmaxCaseNo='000000'+maxCaseNo;
        String suffix=lists0.suffix__c;
        //尾語がNullなら空にする
        if(suffix==null){
            suffix='';
        }
        //左に自動シフトするように.right(6)をつける
        String CaseNo=lists0.prefix__c+newmaxCaseNo.right(6)+suffix;
        update lists0;
        system.debug(CaseNo);
        return CaseNo;

    }
0
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
0
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?