You need to sign in to do that
Don't have an account?

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
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!
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!
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.
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.