You need to sign in to do that
Don't have an account?
nidhi 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?
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?
If this helps,please mark it as best answer to help others :)
All Answers
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
If this helps,please mark it as best answer to help others :)
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
Just to reconfirm hash function is generating a unique number evrytime?
Happy to help !!
Can i use this code to generate an alphanumeric value?
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.