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
AstroCRMAstroCRM 

Random number generation?

Hi all,

I have a bit of a problem here, I need a field that generates an random 7 digits number when a record is created. Can anyone think of a way on how to do this?

Any help would be highly appreciated.



SteveBowerSteveBower
Several thoughts:

1. Override NEW to call an S-Control that creates the object, generates a random number (with Javascript Math.random() )  into a field, saves the object, and redirects to the Edit page for that object id.

2. If you want it all in the Formula field, you could instead hack together some sort of pseudo random generator.  You've got several "random" numbers at your disposal already to use as a key, the ID of the Object being the most obvious one, as well as the time value in the  "Creation Date" field.  Some sort of weak algorithm like middle squares would probably be sufficient depending on how many records you're creating and what your tolerance for duplicates is.

3. Lastly, and perhaps easiest, the Salesforce ID for the record you've just created may not be random, but it is supposed to be unique.  Is uniqueness sufficient to your need?  If so, perhaps just use a subset of that ID number.

Best of luck, Steve.



Praveen kumar 61Praveen kumar 61
use the before insert logic in trigger write a small code like below:

Long key = crypto.getRandomLong();
if(key>-100000000 && key<10000000)
     key = key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key;
if(key>-100000000 && key<10000000)
     key = key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key;
if(key>-100000000 && key<10000000)
     key = key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key+key;
String pin = key+'';
Reference.FieldName__c = pin.substring(1,8);