I have to generate a Unique number for every record in a object.I can't use the Auto-number and SFDC ID of the record.

It's a kind of External ID that external System can use to identify the records.

  • 10
    Can you explain why you can't add just an extra auto-number field to do this? That is by far the simplest solution. – Keith C Aug 8 '14 at 18:08
  • is there some particular format that the external id must adhere to? – cropredy Aug 8 '14 at 18:34
  • Do you care if they're in order? Or just that they're unique? – sfdcfox Aug 8 '14 at 18:45
  • Similar question/answer can be found here: salesforce.stackexchange.com/questions/4289/… – Jenny B Aug 8 '14 at 23:20
  • @jjbennett530 Numbers with no gaps - the focus of your link - is extra hard; unclear yet whether that is required or not. – Keith C Aug 9 '14 at 0:25

If you really, really need to do this, you could try this:

String s = record.createDateTime.format(); // or some other datetime

Blob hash = Crypto.generateDigest('MD5', Blob.valueOf(s));
String uniqueString = EncodingUtil.convertToHex(hash);

return uniqueString;

You'd need to use it in an oncreate trigger and update the record with this value. If the string was too long, you could just chop it off at some point using theString.substring(start,numChars);

| improve this answer | |
  • I have a similar problem, with the parallel concurrence in the creation of of a custom setting record. Has been violated the the unique name rule and has been created two records with same name, which create problems in update etc... – Klodj_Meta Oct 30 '14 at 12:43

Elaborating on Caspar Harmer's answer:

Blob hash = Crypto.generateDigest('MD5', Blob.valueOf(s)); is very expensive CPU Time wise.

The following test timed out for me, as did a loop of 1000:

private static void testIdUniquenessMultipleCalls  () {
    Set<String> ids = new Set<string>();
    for(Integer i = 0; i < 10000; i++) {
    system.assertEquals(10000,ids.size(),'consecutive calls must produce distinct Ids');

I always received a CPU Time Limit exception.

Here's a version that passes the test:

public static String getUniqueId(){
    String milliseconds = String.ValueOf(System.Now().getTime()); //unix timestamp
    Blob pseudoHash = Blob.ValueOf(milliseconds + String.ValueOf(Crypto.getRandomInteger()));
    String uniqueString = EncodingUtil.convertToHex(pseudoHash);
    uniqueString = uniqueString.length() > 255 ? uniqueString.substring(0,255) : uniqueString;
    return uniqueString;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.