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
bjohnsonbjohnson 

Reset Autonumber Daily

Hello,

I'm trying to figure out the best way to do this. I would like to have an autonumber in the format [mmddyy]000. For integration purposes the numbers cannot go over 999 and most preferably these numbers would be reset daily. I know that if you turn the field into text then back to autonumber then it will reset. Of course I wouldn't want to do this manually daily.

The way I see it I have a few options. Use an Apex trigger or if possible, use the api to change the field to text then back to auto number and schedule it to run at midnight. I would think that there would be a better way than these 2. Any suggestions? Does anyone know if it is possible to change the field type programatically?

Thanks
bjohnsonbjohnson
I didn't find anything on changing field types in the API. I'm now 95 percent sure that it can't be done.

I am able to do this though using an apex trigger. This just counts the number total orders for the day and appends to the date.

Just for practice I did it on my Contact object in my developer account. I checked for the text field to be unique when creating so that it will fail on simultanious additions.

The only concerns I have left now is figuring out how to deal with the time difference for GMT to PST.

trigger ContactAdd on Contact (before insert) {

Contact c = (Trigger.new)[0];
AddNewContact.addContact(c);

}

public class AddNewContact {

public static void addContact( Contact c ) {
String pad = '';
Integer contactCount = [select count() from Contact where CreatedDate = TODAY];

if ( contactCount < 100 ) {
pad = '0';
} else if ( contactCount < 10 ) {
pad = '00';
}

c.OrderNumber3__c = (System.today().format()).replace('/', '') + pad + contactCount.format();


}


}