+ Start a Discussion
Chris TquilaChris Tquila 

Unit Test increments auto number in Live !!

I don't have much credit finding this weird thing but I wanted to know if it's a bug or if it's really intended, which I cannot imagine. 

copy paste from this blog: http://blog.tquila.com/2012/06/25/grokking-force-dot-com-apex/

 

"Unit Tests increment your Objects unique Id

This is not covered in the documentation, or atleast I haven’t found it in the Apex docs. When you run unit tests that insert/create records for a particular object(standard or otherwise), it increments the value of the Name field(of type Auto Number) outside of the test context.

Let’s say you have a custom object “Invoice Statement” (Invoice_Statement__c) that has a Name field called “Invoice Number”, and is of type “Auto Number”. The format of the field is: INV-{0000}, and let’s assume that the latest record number is INV-2058.

If you insert 10 records in the test class as follows:

 

1// Let's assume that this runs inside of a test method
2List<Invoice_Statement__c> invStatementList = new List<Invoice_Statement__c>();
3for(Integer i = 0; i < 10; i++) {
4  invStatementList.add(new Invoice_Statement__c(company='acme ltd');
5}
6insert invStatementList;

 

Now, when you insert a new Invoice Statement record outside of the test method via a trigger, or batch, or the User Interface, the next inserted record will have the id INV-2069. So don’t rely on the unique name field if you have strict rules around the auto-increment feature in your application. Instead add a custom field which is auto-incremented in a workflow or trigger to have more granular control over how the value is incremented."

 

I verified that by myself, and this indeed happen. I remember having used these numbers for customer who want to have exactly one unique number per record (especially for bills), so this could be a huge issue.

 

Anyone aware of that? I didn't find it in the Salesforce known bug ....

SeAlVaSeAlVa

I believe that you need to log a Case to Salesforce to change that behavior

InternalServerErrorInternalServerError

Ask support to enable "Separate autonumbers for apex test"

Chris TquilaChris Tquila

InternalServerError wrote:

Ask support to enable "Separate autonumbers for apex test"


really, it is something to enable? I don't understand why it's not default enabled because I cannot imagine anyone who wish to see its autonumber incremented through a unit test!..

 

Anyway, I will give a try, thanks!

vinodshivharevinodshivhare

Chris, Did  you get any support from Salesforce on this issue. Is there something to enable "Separate autonumbers for apex test"? Please let me know, I am also facing this issue. Thanks in advance..

Chris TquilaChris Tquila

it seems it worked yes! Though they don't guarantee that the sequence will be respected...

MaddySinghMaddySingh
This thread is old however it may help someone else. 

I believe you are looking for this option in Setup>Apex Test Execution>Options>  Independent Auto Number Sequence


User-added image
Beer NutthawanBeer Nutthawan
Hi MaddySingh 

Does it work for the change set? 

Best regards
Nutthawan P