+ Start a Discussion
kri 10kri 10 

Auto generated number using Apex class.

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

Thanks in advance
nitesh gadkarinitesh gadkari
Hi,
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.

Regards
Nitesh    
sslodhi87sslodhi87
Hi

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 : https://developer.salesforce.com/blogs/developer-relations/2013/07/selecting-random-numbers-and-records-on-the-force-com-platform-part-1.html

Please let me know if this works for you.

Thanks