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
Bh_5Bh_5 

3 digit Identifier [0-9][A-Z][A-Z] for Salesforce Record

Hello Everyone,

Need help on generating 3 char Identifier for Salesforce Record in below format :
[0-9][A-Z][A-Z]
e.g. 0AA,0AB,0AC....0AZ,0BA,0BB........9ZZ

Do we have library methods to generate such combinations ?
If not Please help in implementing the algorithm to generate 3 char Identifier for each of the records.
Best Answer chosen by Bh_5
Bh_5Bh_5
Thanks everyone for sharing the approach. I just found one link, just in case it could help others
https://stackoverflow.com/questions/16822034/java-library-for-character-sequence-generator
Though above link is not for APEX but it exactly provides solution to me

Bhanu

All Answers

Avishek Nanda 14Avishek Nanda 14
Hi Bhanu,
 
Integer len = 3;
Blob blobKey = crypto.generateAesKey(128);
String key = EncodingUtil.convertToHex(blobKey);
String generatedKey = key.substring(0,len);
System.debug('************ '+generatedKey);

the variable lens is the desired length of the string generated.
It is possible that it may return the only number. You can add the extra condition that aftre random string generation check that it contains atleast one string otherwise again generate it.
Bh_5Bh_5
Thanks Avishek for the response
but their could be scenarios where the returned value from above statement could be duplicate and then i have to additionally query the records to check if its been set on any record.

Can't we have in the sequence [0-9][A-Z][A-Z]
e.g. 0AA,0AB,0AC....0AZ,0BA,0BB........9ZZ
 
Avishek Nanda 14Avishek Nanda 14
Correct We have to check the duplicates by adding the logic. I don't think we can have it any other way except Apex. 
Could you tell me the use case what you are trying to achieve so that I can suggest any alternatives?
Shubham saini 14Shubham saini 14
Hello Bhanuprakash,
 
String str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
String res = '';
for(integer i = 0; i<10; i++){
    for(integer j = 0; j < str.length(); j++){
        for(integer s = 0; s < str.length(); s++){
            res = res+i+str.substring(j,j+1)+str.substring(s,s+1);
        }
    }
}
System.debug(res);

I hope this will help you,

Regards

Shubham
Bh_5Bh_5
Thanks everyone for sharing the approach. I just found one link, just in case it could help others
https://stackoverflow.com/questions/16822034/java-library-for-character-sequence-generator
Though above link is not for APEX but it exactly provides solution to me

Bhanu
This was selected as the best answer