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
caterina ferrigno 8caterina ferrigno 8 

How to create an Auto Number

Hello all! 
I have a standard object "Account" with a custom field(picklist) "Stato";
I want to create a custom field "Nome account" where the value is an automatic incremental number that is assigned when the picklist value of "Stato" is changed to 'Cliente',  if that number was not already present.
For example "AC 000000" where "000000" must be an autonumber.

Can you help me?

Thank you very much!
Best Answer chosen by caterina ferrigno 8
ManojSankaranManojSankaran
Hi Caterina,

This can be achieved in two ways.
1. Using Process Builder + Flow
2. Using a Before insert Trigger + Custom Settings

Below is the logic for using before trigger.

Before update

CustomSettings__c cSettings = new CustomSettings__c();
cSetting = [select latest_Running_Number__c from CustomSettings__c LIMIT 1];
Integer LatestNumber = cSettings.Latest_Running_Number__c;
for(Account acc : Trigger.New){
     if(acc.Picklist__c == 'Stato'  &&  Trigger.OldMap.get(acc.id).Picklist__c == 'Cliente'){
            acc.Auto_Number__c = LatestNumber + 1;
     }
}

cSettings.Latest_Running_Number__c = LatestNumber;
update cSettings.


If you want to implement the same in Process Builder and Flow.
1. Create a process builder with the same condition
2. Use Fast Lookup to get the Custom settings value
3. Increment the value and use fast update,


Mark it as answer if this really help you solve the issue.



Thanks
Manoj S



 
           
     

 

All Answers

Dileep KumarDileep Kumar

Hi,

Please create auto Number field on account.

Display Format : AC-{000000}
Starting Number : 0

Please try it and let me know.

Thanks,

Dileep

ManojSankaranManojSankaran
Hi Caterina,

This can be achieved in two ways.
1. Using Process Builder + Flow
2. Using a Before insert Trigger + Custom Settings

Below is the logic for using before trigger.

Before update

CustomSettings__c cSettings = new CustomSettings__c();
cSetting = [select latest_Running_Number__c from CustomSettings__c LIMIT 1];
Integer LatestNumber = cSettings.Latest_Running_Number__c;
for(Account acc : Trigger.New){
     if(acc.Picklist__c == 'Stato'  &&  Trigger.OldMap.get(acc.id).Picklist__c == 'Cliente'){
            acc.Auto_Number__c = LatestNumber + 1;
     }
}

cSettings.Latest_Running_Number__c = LatestNumber;
update cSettings.


If you want to implement the same in Process Builder and Flow.
1. Create a process builder with the same condition
2. Use Fast Lookup to get the Custom settings value
3. Increment the value and use fast update,


Mark it as answer if this really help you solve the issue.



Thanks
Manoj S



 
           
     

 
This was selected as the best answer
caterina ferrigno 8caterina ferrigno 8
Thank you Manoj for your answer!
Your logic in a trigger works well, but I would to create a similar logic in an apex class and then recall it in a trigger. 
If I write your logic in an apex class obviously I make mistakes that I don't know how to solve. 
Can you help me also this time? 

Sorry, and thanks anyway!
 
caterina ferrigno 8caterina ferrigno 8
This is my logic in the trigger:

trigger AccountTrigger on Account (before update) {

            CustomAccount__c cAccounts = new CustomAccount__c();
            //CustomAccount__c is the name of my custom setting

            

            CustomAccount__c cAccount = [SELECT CustomCounter__c  from CustomAccount__c LIMIT 1];
            //CustomCounter__c is a field number  of CustomAccount__c


                  Integer LatestNumber =Integer.valueOf(cAccounts.CustomCounter__c);

                     for(Account acc : Trigger.New){
                         if(Trigger.OldMap.get(acc.id).Stato__c == 'Cliente'){
                            acc.Numero_Account__c = 'AC LatestNumber' + 1;
                            //Numero_Account__c is a text number of Account
                             }
                     }

              cAccounts.CustomCounter__c= LatestNumber;
              update cAccounts;        
           
         
              
}

If I create a New Account and I change the Status value to 'cliente' , the field "Numero Account" of Account is blank :( 
Where am I wrong?
caterina ferrigno 8caterina ferrigno 8
I've solved! 
Thanks!
Felipe PeñarandaFelipe Peñaranda
and how is the concurrency resolved?
Alexandru Catalin Cristea 1Alexandru Catalin Cristea 1
In a concurrent situation you may end up with the same number for multiple objects.
As possible solutions:
1. add autonumbered field on Account and copy its value on after insert trigger - as many suggested already;
2. create a custom object (helper object) with one autonumbered field that you can create just when a new number is needed and you can delete it afterwards to free storage space.