0

I am working on a trigger which should generate grant contract numbers according to a particular format. For each RT:

RT starting Number  Ending Number
WF    2300021   2301999
SAT   2304020   2306999
Exp   2330001   2349999
Youth 2370120   2370999
DYS   2371025   2371999
Exec  2396110   2396199

So if I create a new grant contract number of a particular RT then the grant contract number should start with the starting number and go on incrementing on each record creation of that particular record type.

Similarly it should work for all record types. Here 23 represents the last two digits of the fiscal year which I have captured using a formula field and stored in the trigger. This I should concatenate with the remaining part and for a new record the number should increment.

For eg:if I create the first WF grant contract then the number would be 2300021 and the next time it should be 2300022 and should go on till 2301999.

I have started the code but am a bit confused in the incrementing part.

Trigger:

trigger Generate_Number on Grant_Contract__c (after insert) {
    List<String>lstAcc = new List<String>();
        For(Grant_Contract__c c:trigger.New){
            String f=string.valueof(c.Fiscal_Year__c).right(2);
            String fi;
                System.debug('f' +f);              
            If(c.RecordTypeId=='0128F00000006rRQAQ'){
                 fi=f+'00021';
                System.debug('fi' +fi);
                 Integer intval=Integer.valueOf(fi);
                intval++;
                String valString=String.valueOf(intval);
            }
                Integer month = c.CreatedDate.month();
                if(month>6){
                     fi=f+'00021';
                    System.debug('fi' +fi);
                    Integer intvalue=Integer.valueOf(fi);
                    intvalue++;
                  String valString=String.valueOf(intvalue);
                }
            if(c.RecordTypeId=='0128F00000006rQQAQ'){
                 fi=f+ '04020';
                System.debug('fi' +fi);
                Integer intvalue=Integer.valueOf(fi);
                intvalue++;
            }
            lstAcc.add(valString);
        }
    update lstAcc;
}

NB: Grant contract number is a text field.

2
  • Did you think of the part where you need to remember 'where you left off last time' when a new record is created? Jul 25, 2022 at 12:27
  • @GuyClairbois Yes that is also a challenge.
    – PJ98
    Jul 25, 2022 at 12:48

2 Answers 2

0

Although this is quite a common use case, it is not as simple as it might seem. You need to deal with multiple recordtypes, with continuing each range from the last point where you left off, with records being created in bulk, etc.

There's a quite nice solution description here, provided by Piyush Singhal: https://hicglobalsolutions.com/blog/salesforce-apex-triggers-generate-auto-numbers-records/

2
  • This seems informative but in my case I cant create autonumber fields as they have done because I have to add an extra field just for this process. Is it possible to achieve through just a trigger.
    – PJ98
    Jul 25, 2022 at 14:45
  • I don't think that solution is using autonumber fields. The difference is that they use 1 number range and you have separate ones. So you'll need to split up the query results into recordtype groups and define the maximum last number per group. Jul 26, 2022 at 7:23
1

An additional custom object can help, so you can create records named as rt names and add a Number field with the number of the last record. after all objects are created, you update records with the last number

1
  • Welcome to Salesforce Stack Exchange (SFSE)! A previous employer had very complex criteria for custom Account Numbers based on geographic (global) location, Record Type, and one other item I can't recall exactly what was - then appended with a unique number. They wanted the unique number of each new Account Number to start with 00001 for the first record created, and then be consecutively numbered for each one meeting all the same other criteria. (E.g., US-ABC-00001, NZ-XYZ-00001, US-XYZ-00001, US-ABC-00002.) We used a custom object to keep track of the last number used for each one.
    – Moonpie
    Jul 25, 2022 at 15:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .