back to blog
Skip Auto Number for specific values
December 10 2021 • 5 min read


Auto Number field type in Salesforce is a system-generated sequence number that uses a display format you define. For each new record created, the number automatically increments.

The starting number defines the initial number which will be generated in the given format for the first record that will be created.

Let's assume you want to skip a specific auto number generated from being assigned to a record being created.

We can achieve this using an After Insert trigger.


  • Let us create a field named Record Sequence Number which is an AutoNumber field type.
  • The display format can be {000000} or {0000} or {00} etc...
  • Let the format be OP-{0000}.
  • If the starting number for the format OP-{0000} is 1, then the initial AutoNumber generated would be OP-0001.
  • Now, I want to skip the Auto Numbers ending with zero.


The code for the trigger is as follows

trigger AutoNumberTrigger on Opportunity (after insert) {
    for (Opportunity {
        String[] strArray = opp.Record_Sequence_Number__c.split('\\-');
        Integer num = Integer.valueOf(strArray[1]);
        Id oppRecordType = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('New').getRecordTypeId();
        Opportunity opty = new Opportunity(Name='TestName', StageName ='Qualification', CloseDate =, Quantity__c=200, RecordTypeId=oppRecordType);
        if (math.mod(num + 1, 10) == 0) {
            Opportunity opp = opty.clone(false, true, true, false);
            insert opp;
            delete opp;
  • For every record we create, after Insert we will check if the next Auto Number value (current value +1) is divisible by 10.
  • If it is divisible by 10, we insert a Dummy opportunity into the Object, this dummy Opportunity will have the Auto Number value that ends with 0 and then we delete it from the Database.
  • Since the deleted record has an Auto Number value which ends with 0, the next record will have the Auto Number value which is an increment to the deleted record value.
  • This way, we can skip the unwanted Auto Number values from being assigned to the records.


This technique may not work if you are inserting records in Batches.


In this blog we have covered how to skip the Auto Number value from assigning to a record using an After Insert Trigger.

Leave a Comment

Your email address will not be published

© 2024 Digital Biz Tech