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
Scott.MScott.M 

Tests Affecting Production Auto Increment Numbers

Hi All,

 

We have a custom application that makes use of an auto increment field. It's critical that the numbers are sequential and accounted for. The problem is that every time the unit tests get run they affect the auto increment number. This shouldn't happen, tests should not affect production data including auto incremented numbers.

 

In the idea exchange there a few others that ran into this same problem and apparently salesforce turned something on to fix it for them. Salesforce support has told me twice that it's not available. Any ideas on how I can fix this other than reseting the number every time an application is installed or updated? 

 

Thanks!

Scott 

sfdccoder1sfdccoder1

 

You can implement a "Sequence" object, like the one used in Oracle for instance.

 

To do this with Apex you need to:

 

1. Add a custom field to the object you need the auto increment for called "Sequence". 

1. Define a new custom object called "Sequence 1" with one numeric field called "Next Sequence Number".

2. Create a single "Sequence 1" record with "Next Sequence Number" = max(existing auto number + 1).

3. Add a before insert trigger for your object which:

 a. Copies the value of the "Next Sequence Number" field in the single "Sequence 1" record you created to the "Sequence" field in the record in context.

b. Increment the "Next Sequence Number".

 

 

Of course there are issues you need to take care of like data fixing existing records and running the trigger for an array and not just for a single record.

 

HTH.

 

 

Message Edited by sfdccoder1 on 06-16-2009 05:50 AM
Scott.MScott.M

Thanks! That's a great idea  :)

 

I was hoping to avoid doing custom development for this but if all else fails this is what I'll do. Part of the reason I'm waiting to see if Salesforce can do anything is that some people have claimed to have had this fixed in their organizations in this idea:

 

http://ideas.salesforce.com/article/show/10095903/Auto_Numbers_should_not_increment_with_Run_Tests