What does Salesforce ID compose of? and How to Convert 15 Character Id to 18 Character Id

Id Field Type is a base-62 encoded string.
Each character can be one of 62 possible values:
  • a lowercase letter (a-z) - 26 values
  • an uppercase letter (A-Z) - 26 values
  • a numeric digit (0-9) - 10 values
Within a 15 character Id the breakdown is: (CASE SENSITIVE)
  • First 3 characters -
    First 3 characters are the key prefix that identify the object type.

    In Case of Standard objects: Same Unique key prefix across all the orgs. (very useful in case you have hardcoded standard object id in apex, so now you won't need to change it when deployed in other org)
    In Case of Custom objects: Unique key prefix per Org, but have a different key prefix in each installed org. (How to get object key prefix)
  • Remaining 12 characters - These is a 62^12 number. It is common to see the first character in this sequence set to a value other than 0 even though most of the following characters are zero. This appears to be some kind of offset and may have significance.
    Review -a comment from @ca_peterson says that first character is a pod identifier.
18 Character Id: (CASE INSENSITIVE)
  • Salesforce.com IDs are case-sensitive so if you wanted to export data to Excel to be used as a lookup table using the VLOOKUP formula, you’ll need to be aware of this. If you happened to have records with IDs of 001A0000006Vm9r and 001A0000006VM9r the VLOOKUP formula, not being “case-aware”, would always find the first record, regardless of the case of the lookup value. Salesforce.com realized the potential problem with this and instigated the 18-character ID.
  • So overall in 15 character Id optional 3 character suffix is added which would make sure that Id becomes case insensitive.


Convert from a 15 character Id -> 18 character Id :

1) Using algorithm:

  1. Divide the 15 char into 3 chunks of 5 chars each.
  2. For each character give that position a value of 1 if uppercase, 0 otherwise (lowercase or number).
  3. Combine the bits from each chunk into a 5 bit integer where the rightmost bit is the most significant bit. This will yield a number between 0 and 31 for each chunk.
  4. Construct an array that contains the sequence of capital letters A-Z and 0-5 (26 + 6 = 32 possible values).
  5. Use the integer from each chunk to choose a character from the array.
  6. Append the resulting 3 characters, in chunk order, to the end of the 15 char id.

2) Using formula CASESAFEID () function

----------------------------------------------------------------------------------------------------------------------------------

Note:

RegEx to validate Salesforce Id:
[a-zA-Z0-9]{15,18}
Anonymous Apex to determine type from Id:
System.debug(System.LoggingLevel.ERROR, Id.valueOf('0EM100000000111').getSObjectType());

Comments

Popular posts from this blog

List of Key Prefixes in Salesforce

SFDX Install CPQ in Scratch org