What are Salesforce ID's composed of?

  • I remember seeing somewhere that IDs are composed of a few pieces. I always have a hard time trying to find that information when I'm looking for it. What I mean by the above is that the various places in the ID represent different things - for example the first few characters represent what type of sObject it is.

    Awesome question - especially the valuable data it uncovered by virtue of the replies below. ;)

  • The 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

    As there is a combination of lower and upper case letters the casing of the 15 character Id has significance. E.g. 50130000000014c is a different ID from 50130000000014C.

    Within a 15 character Id the breakdown is:

    • First 3 characters - Key Prefix As per Jon's answer, the first 3 characters are the key prefix that identify the object type. There are a few exceptions to this where multiple objects all share the same key prefix! There are a number of fixed key prefixes that are common across all of Salesforce. Custom objects get a unique key prefix per Org. I'd need to confirm this, but I'm fairly certain that Custom objects in managed packages can have a different keyprefix in each installed org.
    • The 4th and 5th characters - Reserved. Currently used for the instance id (a.k.a. pod identifier) (As per comment from @ca_peterson). Starting with the 4th character and overflowing to the 5th if required. Indicates which pod/instance the record was created on. Note that data may be migrated to other pods over time. Updated based on a separate question that indicated the pod identifier is actually two characters rather than one as initially thought.
    • 6th and 7th characters - Reserved. Will be 0 until such time that Salesforce has a need for it. Source - Steven Tamm
    • Remaining 8 characters - basically a really big number. Like 62^8 big.

    To this you can add an optional 3 character suffix that will make the Id unique case-insensitive. This is useful when working with programs that can't maintain the case of the ID (E.g. Excel VLookup).

    Notes about the suffix:

    • this is not intended as a check sum to verify the other 12 characters haven't been corrupted.
    • you can't just lower/upper case the entire ID. While it helps other case insensitive applications handle the IDs Salesforce is still case sensitive and won't auto correct the casing based on the suffix. E.g. the casing on a KeyPrefix is important with 00t being OpportunityShare and 00T being Task.

    The algorithm to convert from a 15 character Id to an 18 character Id is: (Source - I'm sure there used to be official documentation on how do this.)

    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.

    In a formula there is the CASESAFEID function that will perform this algorithm.

    You can apply this algorithm to some sample IDs to see how it doesn't really function as a checksum or checkdigit. For example, if you exclude the alpha characters, every ID between 001100000000001 and 001999999999999 will have the suffix AAA. Infact, you get the same suffix if you include any lowercase alpha characters as well. The suffix will only change in the presence of uppercase characters. It is basically encoding which of the 5 characters that each suffix character represents are uppercase.

    Sample code to restore the casing from an 18 character ID is in Creating a link using an 18 character ID

    If you are working with Data Exports you can also come across the special empty key with the 000 keyprefix.

    One area I'm not sure of is the order in which Salesforce increments through the base 62 encoding. E.g. Does it go 0 to 9, then a to z, then A to Z? At this stage I think the sequence looks like '0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ' '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

    The first character after the prefix is a unique number based on the pod it was created on (e.g. NA1, CS5). This doesn't correspond to it's name (CS 5 isn't 5), but instead the order of all pods which it was brought online.

    @ca_peterson Interesting, thanks. Might be useful to identify which records were refreshed into a sandbox and which were created locally.

    Rumor has it they're at the max number of pods for a single base-62 encoded digit, so this might spill over into a 2nd character soon.

    Fun fact: you can get a mapping of the pod identifier in the 4th char of the orgId to the pod's name from https://github.com/ryanbrainard/forceworkbench/blob/e7c6507c3903ebddb813a472ff6c59400fd9656a/workbench/config/defaults.php#L219

    This whole case-insensitive thing with the checksum characters at the end (18 character ID) is news to me. WOW. That would negate an annoying formula I've used for years in Excel to turn 15-character IDs into something useful for VLOOKUP, COUNTIF, blah blah blah purposes. THANK YOU for that info, ;-) I just never new adding the checksum had that added purpose/functionality!

    I created a Ruby gem to handle conversion of one id to/from case-sensitive and case-insensitive format. The implementation is actually in C and uses only standard library (you can easily strip out the ruby part), hope someone will find it helpful: https://github.com/Fire-Dragon-DoL/salesforce_id

    Pretty close to 100 votes! I'm curious; I've seen a couple questions recently about the case-sensitivity aspect. Specifically people are tripping over how Ids are case-sensitive *unique*, but that does not mean an improperly cased Id will work anywhere. Do you think that information is on topic for this question? And do you think it fits in the post or would you welcome a new answer outlining that aspect?

    @AdrianLarson I covered the case-sensitive unique part in the middle briefly and mostly by linking to another question that also includes a simple implementation to restore the casing from the suffix. I could expand it slightly, or you could add your own answer if you like.

    I'll think on it a bit. Maybe I'll post on meta to see what others think, too.

    "you can't just lower/upper case the entire ID. While it helps other case insensitive applications handle the IDs Salesforce is still case sensitive and won't auto correct the casing based on the suffix. E.g. the casing on a KeyPrefix is important with 00t being OpportunityShare and 00T being Task." -- I think this is misleading. All the places I know (e.g. SOQL queries) will take an 18-character ID regardless of case and understand it correctly using the case suffix.

    WOW. I love this. I did not know about the Pod reference.

    @ca_peterson That list of instances-to-keys is no longer complete.

    @CharlesKoppelman You would need someone internal to Salesforce to compile a complete list. I've started a new question so the community can build up a more complete list http://salesforce.stackexchange.com/q/164554/102

    @DanielBallinger At the end of the answer you state the ordering sequence is `0123456789ABCEFGHIJKLMNOPQRSTUVWXYZabcedefghijmpnopqrstuvwxyz` I notice there is no 'D' between 'CE' and extra 'e' between 'cede'; no 'kl' between 'jm'; extra 'p' between 'mpn'. Are those typos or correct? Thanks!

    Another good way of converting 15 Ids to 18 is using "Id.valueOf(_15charstringid_)"

    Some additional ID trivia: instanceOf ID seems to only check string length & that the character at position 5 is '0': System.debug(('[email protected]' instanceOf Id)); // true

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used