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
nidhi aryanidhi arya 

Generate Unique Random Number

 Hi,

I have a requirement with my client wherein we are required to generate a unique random number on creation of a record in the system.

To cater to which I have written a below line of code-

integer max=6;
string password=EncodingUtil.ConvertToHex(crypto.generateAesKey(128)).substring(1,max);
recordToUpdate.put('Seller_Code__c'),'S'+password);

this code works fine.But not sure whether this code will generate a unique random number everytime.Can anyone help me with this?

In case if this code does not generates a unique random number how can I handle through same code?

Best Answer chosen by nidhi arya
Vinit_KumarVinit_Kumar
If you want to do this with code ,try below :-

String hashString = '1000' + String.valueOf(Datetime.now().formatGMT('yyyy-MM-dd HH:mm:ss.SSS'));
Blob hash = Crypto.generateDigest('MD5', Blob.valueOf(hashString));
String hexDigest = EncodingUtil.convertToHex(hash);
system.debug('##########' + hexDigest );

If this helps,please mark it as best answer to help others :)

All Answers

Daniel B ProbertDaniel B Probert
take a look at this: https://success.salesforce.com/answers?id=90630000000h0S9AAI

you can do this with formuals and fields in salesforce without the need for code or check out this:

http://www.codebycody.com/2012/02/sfdc-randomizer.html
Vinit_KumarVinit_Kumar
If you want to do this with code ,try below :-

String hashString = '1000' + String.valueOf(Datetime.now().formatGMT('yyyy-MM-dd HH:mm:ss.SSS'));
Blob hash = Crypto.generateDigest('MD5', Blob.valueOf(hashString));
String hexDigest = EncodingUtil.convertToHex(hash);
system.debug('##########' + hexDigest );

If this helps,please mark it as best answer to help others :)
This was selected as the best answer
nidhi aryanidhi arya
Hi Daniel.

Thanks for your input.

But earlier  in our org ,concept of creating random number was implemented  using auto number field. But management was not ok with it.As per requirement 6 digit alphanumeric number should get generated.

However I have managed to solve that problem via above code. But I am not sure it will generate a unique number everytime.

Would need you to cross check my code segment.can you let me know this will return a unique alphanumeric character everytime or not?

Looking forward for your reply.

Thanks,
Nidhi


nidhi aryanidhi arya
Thanks Vinit.Your solution worked.

Just to reconfirm hash function is generating a unique number evrytime?
Vinit_KumarVinit_Kumar
Yes Nidhi,it would generate a unique no everytime.

Happy to help !!
HITESH GODHAHITESH GODHA
Hi Vinit, can we achieve the same with formula field? Because our client don't want custom code for this.
Avinash.BabuAvinash.Babu
HI Vinit, 

Can i use this code to generate an alphanumeric value?
Eric LuuEric Luu
Hi Vinit,

This is awesome! I was able to use this to generate a token for my use case. However, this is not truely unique. If you create records in batch, there may be similar hash. One trick I used was instead of hashString = '1000'..., we could use a counter variable and for loops to count up everytime we enter the loop.
 
Integer counter = 0;
for(<SOME_OBJECT> i:<SOME_OBJECT_LIST>){
    String hashString = counter + String.valueOf(Datetime.now().formatGMT('yyyy-MM-dd HH:mm:ss.SSS'));
    Blob hash = Crypto.generateDigest('MD5', Blob.valueOf(hashString));
    String hexDigest = EncodingUtil.convertToHex(hash);
    system.debug('##########' + hexDigest );          
    i.token__c = hexDigest;
    counter++;
}
Gary HatfieldGary Hatfield
This is fantastic! This enabled me to produce a token for my use case. This is not, however, truly unique. There could be a comparable hash if you create records in batches. One trick I did was to use a counter variable and for loops to count up every time we entered the loop instead of hashString = '200.
Gary HatfieldGary Hatfield
However, the idea of generating random numbers was first implemented in our organization utilizing the auto number field. However, management did not approve of it. The produced six-digit alphanumeric number must meet the requirements.  However after playing on the platform https://goldenstarcasino.bet/ and using the code above, I was able to fix that issue. However, I'm not sure if it will consistently provide a different number. Would like your help double-checking my code snippet.Can you tell me whether or not this will always yield a different alphanumeric character?