I want to know whether this code can create multiple instances of the same random number by any chance? Can i make this specific field to not have same values within salesforce it self?

trigger token on Certification__c (before insert) {

    for( Certification__c c: Trigger.new ) {
        final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        String guid = '';
        while (guid.length() < 8) {
            Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length());
            guid += chars.substring(idx, idx+1);
        }
        c.Name = guid; 
    }    
}

@@@EDIT - So i thought of adding a couple of code lines, which will check the token field c.Name whether if it has this random string value, and if it does generate new random string, if it doesn't set that string. Is this better?

  • 3
    You can use System.now().getTime() which will return numbers which you can add at the end of chars string. That's how you will end up a different number every time. – Naval Sharma Oct 11 at 11:13
  • I want the string to be no more than 6-8 digits. – Niveth Kumar Oct 11 at 11:18
  • check my @@@EDIT – Niveth Kumar Oct 11 at 11:30
  • If you use 36 characters to choose from and you generate a random string of 8 characters long you will have 36 ^ 8 (2821109907456) different values, each occurring with the same probability. You can determine yourself if you like those odds or not. To improve the odds you can increase the number of characters to choose from (ie; make your string case sensitive and add 26 lower case letters) or increase the lenght of the string. – rael_kid Oct 11 at 11:36
  • adding a couple of code lines, which will check the token field c.Name whether if it has this random string value, and if it does generate new random string, if it doesn't set that string. Is this better? – Niveth Kumar Oct 11 at 11:38

Yes there is a small chance of duplicates but the odds may be so low that you don't care.

You can define an AutoNumber Custom Field and include that in your logic, but you will have to change your trigger to "after insert" to get the generated value and so will need to perform an update yourself.

Just saw Naval's comment and that is a better approach: the timestamp will only be the same for 1 second so combining that with the random number generation lowers the odds of a duplicate way further.

  • Hi, there should be no duplication what so ever, Is there a way to have an array or a database where i can add all the generated values and check it before when i create a new one. Is it efficient? – Niveth Kumar Oct 11 at 11:18
  • What do you mean it lowers the odds way further when i add the datetime, can it still create a duplicate by some miracle? and how to simplify that value to a max 8digit string – Niveth Kumar Oct 11 at 11:20
  • please check my @@@EDIT – Niveth Kumar Oct 11 at 11:27
  • 1
    @NivethKumar Your @@@EDIT approach sounds like a good one; just make sure you do a single query for all the names. – Keith C Oct 11 at 13:39

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.