function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jgreenejgreene 

Apex tests are incrementing my Auto Number fields

I have an Apex test that creates a new Case. I figured the tests were transparent, i.e. they just did their thing and didn't modify my data or fire any workflow rules. However, I just noticed that they are in fact incrementing the Auto Number fields. Another member of my team was doing a bunch of development over the weekend and so whenever he ran the test suite it created a new case. I came in on Monday and created a new case; the case number was 100 higher than the last case I had created.

Seems like a bug to me... is it supposed to work like this? Any ideas for a workaround?

Jeremy


Message Edited by jgreene on 10-22-2008 10:11 AM
werewolfwerewolf
Case numbers aren't necessarily guaranteed to be sequential anyway, Apex or not, so you probably shouldn't be depending on the sequential-ness of those numbers.  Obviously, under most circumstances they are sequential, but when something grabs a number from the autonumber pool, there's not really any way to "put it back."  That could be an Apex trigger, or it could be a case that failed validation late in the process or during workflow, for example. 
jgreenejgreene
Thanks werewolf. The documentation does state that auto number fields are sequential, but I think the point you are making is that numbers can be "used up" in subtle ways that make them appear to be nonsequential. Point taken.

I still think it would be ideal if the Apex tests could somehow be rigged not to increment these fields. I understand this might not be possible, I'm just putting the suggestion out there. The current behavior is counter-intuitive.

I am really not that concerned if we skip a few numbers. I was worried that we would start skipping hundreds or thousands of numbers, and my case numbers would go from being 4 digits to being 5 digits. I'd rather keep them 4 digits as long as I can :-)

For all practical purposes, I think we have solved this problem: it turns out that one of our developers was developing Apex code directly on the production salesforce.com instance. So the tests were being run over and over in production. He is going develop in the sandbox instance instead. This way the tests won't affect production until the code is deployed from sandbox to production.