Have you ever noticed that your invoice numbers suddenly jump to a higher value, instead of following the usual sequential order? If so, it may be due to a scenario that is described in Auto-number fields value gets skipped while running RUN ALL TEST.

Auto-number fields are commonly used to automatically generate unique and sequential numbers for records in Salesforce. However, if you run a test that contains a record with an auto-number field, the value of that field will be skipped for any subsequent records in the test.

For example, if you have a test that creates two records with auto-number fields, and the first record's auto-number field has a value of "INV-0001", the second record's auto-number field will have a value of "INV-0003" instead of "INV-0002". This is because the value "INV-0002" was skipped when the first record was created.

Sample record In the event that the Independent Auto-Number Sequence is disabled.


To avoid this in the future, please follow the resolution identified in the same link above:

Go to SetupCustom Code | Apex Test Execution | Options, and check the box Independent Auto-Number Sequence.

The auto-number sequence in Salesforce is controlled by the system and is independent of test execution. Auto-number fields are automatically generated by the platform and follow a predefined sequence, incrementing with each new record created.

During the execution of Apex tests, Salesforce may create test records that include auto-number fields. These test records are typically created in a separate data environment, called a test context, and are isolated from the main data records in your org. The auto-number values generated for these test records are distinct from the values generated for production records.