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
RockersRockers 

How to add 6 digit random number id to the inserted record?

Hi Friends,

 

          I want to add 6digits random to the inserted record which will be used as a unique id for the records. For Example,

In Standard User Object, when an user is created, i need add a value which is alphanumeric combination as follows:

 

Format:-

 

 ABC[2digit Country Code][xxxxxx].

 

 So, pls help me to solve this one.

 

Regards,

Phanikumar

Best Answer chosen by Admin (Salesforce Developers) 
stcforcestcforce

 

    double y = Math.random() ; //between 0 and 1

     y = 999999 * y;

     integer index = math.round(y);//something between 0 and 999999

 

you will need to check against exisitng numbers generated.

the exact randomness of the above is partially contingent on the double format.

All Answers

stcforcestcforce

google 'salesforce math methods'. There is a random method that returns a double in the range of 0-1. Mutliple this by your range and then use the round function (also via math methods).

RockersRockers

Thanks for your valuable reply Friend. Let me try and let you know about the result.

 

Regards,

Phanikumar

stcforcestcforce

 

    double y = Math.random() ; //between 0 and 1

     y = 999999 * y;

     integer index = math.round(y);//something between 0 and 999999

 

you will need to check against exisitng numbers generated.

the exact randomness of the above is partially contingent on the double format.

This was selected as the best answer
Ispita_NavatarIspita_Navatar

Hi Rockers,

            You can make use of crypto class to get random integer values.

  Do try the follwoing code it may help you resolve the issue you are trying to solve.

            Integer Rnd = crypto.getRandomInteger();

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

RockersRockers

Thank you Friend...

 

        Its working fine now.

 

Regards,

Phanikumar