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
QuicksilvrQuicksilvr 

Auto generate unique string ID from record name

Hi guys,

I'm looking for the best way generate a unique ID string from a record name.

Example:
Account Name: Universal Containers
Account Unique ID: UNI

UNI should be auto-populated, and has to be unique. If UNI is already associated with another record, then it should be UNV/UNC/UNL etc.

This ID only needs to be unique per territory / team. eg., UNI belonging to one territory, say the UK, is still unique from UNI belonging to the US. In this case I can't use the "Unique" checkbox in the formula field. I can probably overcome that by creating another formula field that copies the country code and the unique ID to check for duplicates.

When checking for duplicates, ideally it would follow this logic -

Account Name: Universal Containers
Country on User (Account Owner): UK

The trigger will populate "UKUNI" using
String ownerCountry = {!User.Country};
String accountName = {!Account.Name};

String UID = ownerCountry.substring(0,2) + accountName.substring(0,3);

Now this needs to be checked for being unique, and insert if it is. This is where I'm lost - if the ID is already taken, it should give me the next possible combo, letter 1,2,4 (UNV) or 1,2,5 (UNE) or 1,3,4 and so on.
Once I have this value, I can copy just the 3 letter code without the country code onto another field. This field will also be user editable, so in a sense, the system generated unique ID is a suggestion which the user can accept or override.


What would be the best approach to check for duplicates and generate the unique ID?

Thanks for you help!
Here-n-nowHere-n-now
How about using the auto-number field type?

Another thought is, does the UID field need to be 5 char long or human readable?  If not, I'd use something like Crypto.getRandomInteger().
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_crypto.htm#apex_System_Crypto_getRandomInteger
If you really want to guarantee uniqueness (without needing code to catch the rare dupe from getRandom), you can also append the current timestamp to it.
QuicksilvrQuicksilvr
Thanks for your reply. The auto-number field will not cut it for my requirement. It needs to be 3 characters long.
I showed the 5 characters long ID because it only needs to be unique per country / sales team (as in my example above), and this is the only way to do it. In the end, I just need the 3 characters.
Here-n-nowHere-n-now
Well if you do need it to be 3-char format, then you many want to try to use a record that remembers the "Last ID" used.  Then whenever you need a new ID, just query the "Last ID" (and lock it), increment it in some fashion and use the new ID, then update the "Last ID" record with the new ID.  This will not work, however, if you need the ID to be related to your account name in some way (like your sample code suggested).