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
kri 10kri 10 

Auto generated number using Apex class.

  Please tell me how to generate auto generated number to display in VF Page using apex code.

Thanks in advance
nitesh gadkarinitesh gadkari
As such you should see if you can create autonumber field.If not,the basic idea would be like this:-

You can make use of Custom Settings.With custom settings,you have control over number being generated without revisiting the code.
You can use this to maintian counter of autonumber.
Also, creat a Text Field to store the autonumber being generated.
Initialize it with 1.Everytime you create a record,stamp the required field with this number and at last increase the number and update the custom settings. In same custom settings you can store some string pattern like ABC-000 which you can use in code and concatenate it to obtain desired string.


You can use below mentioned code to generate number by setting the upperlimt as well
public Integer randomWithLimit(Integer upperLimit){
        Integer rand = Math.round(Math.random()*1000);
        return Math.mod(rand, upperLimit);

For more information you can check below mentioned link :

Please let me know if this works for you.