I have been noticing this and it has happened more than once. I have an object and it's name is the standard auto number
. When I'm creating the records, after a few records, the auto number
seems to break the pattern and skips about 40-50 numbers. I am the only one who is using the org and none of the previous records are deleted. This has happened in the production too. Any explanation for this? My org is in Enterprise Edition
2 Answers
I believe this can happen because of two reasons.
- You are creating or deleting records. (Unlikely)
- You do not have independent test sequence turned on. Due to this whenever someone runs a test class, the Auto-numbers for those test records are calculated starting from last record in production. (Link)
Please enable this from:
Develop | Apex Test Execution | Options..., selecting Independent Auto-Number Sequence, and clicking OK.
New Independent Auto-Number Sequence Test Option
A new option has been added to ensure that auto-number fields in your organization's records don’t have gaps due to test records created in Apex tests. This option isolates the auto-number sequence used in Apex tests from the sequence used in your organization. As a result, the creation of test data in Apex tests doesn't cause the sequence of auto-number fields to be higher for new non-test records in your organization.
Please also check out Phil's answer below. I missed that point
Exceptions during insert can also cause jumps. I think auto-numbers go 'missing' in the same way as the name field does sometimes. For example, consider the following code:
System.Savepoint sp = Database.setSavepoint();
List<ObjectWithAutoNumber__c> objectsToInsert = new List<ObjectWithAutoNumber__c>();
for(Integer i=0;i<50;i++) {
objectsToInsert.add(new ObjectWithAutoNumber__c());
}
insert objectsToInsert;
Database.rollback(sp);
Inserting the 50 records consumes and allocates the auto numbers, but rolling back does not undo that allocation. So, it could be when a legitimate exception has occurred on the insertion of some records at some point.
-
Great point, and missed by the accepted answer. I made some minor tweaks, hope you don't mind. :)– Adrian Larson ♦May 19, 2016 at 16:16