Salesforce IDs Explained (UPDATED)

Everything you ever wanted to know about Salesforce IDs and then some.

Salesforce IDs Explained (UPDATED)
📣
This article has been updated to reflect the changes made in Salesforce's Summer '23 release. Salesforce now uses 3 digits for the Instance ID, rather than 2. This seems to be connected to the implementation of Hyperforce. You can read more about this change here.

The Anatomy of a Salesforce ID

To the untrained eye, a Salesforce ID might seem like a random collection of numbers and letters. However, if you have been around Salesforce for even a short while, you will start to see some patterns. Salesforce IDs are, in fact, made up of district parts. See the infographic below:

Anatomy of a Salesforce ID Infographic
Anatomy of a Salesforce ID Infographic

Object Prefix

id-parts_part1-1

The first 3 characters tell us which SObject type the record is. Most SObjects that users interact with, both standard and custom, have a prefix. In nearly every case, standard objects will have the same prefix in every org. Here are some examples:

SObject Name Prefix
Account 001
Contact 003
Opportunity 006
Lead 00Q
Case 500
User 005

Here are some more.

But rather than trying to memorize all these "magic codes", here are a couple of ways to find out all the prefixes in your org, including custom objects, which will obviously be org specific.

Getting SObject Prefixes:

Using Apex

Here is an almost one-liner to print all of your org's SObjects and their respective prefixes to the debug console:

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
for(String key : gd.keySet()){
    System.debug(key + ' = ' + gd.get(key).getDescribe().getKeyPrefix());
}
Apex Code to Get SObject Prefixes
ℹ️
You can run the code above in the Apex Anonymous window. Simply open up the Developer Console (Gear Icon » Developer Console), and select "Open Execute Anonymous Window" from the "Debug" menu.

Then copy and paste the code snippet and click the "Execute" button.

Keep in mind, some setup SObjects do not have prefixes and will return null

Using SOQL

You can also query this information using SOQL:

SELECT Label, DeveloperName, KeyPrefix FROM EntityDefinition LIMIT 2000
SOQL Query to Get SObject Prefixes

Just note that the EntityDefinition table has a restriction where you can only query 2000 records or less and it does not support pagination. So use a LIMIT or WHERE clause to make sure you stay under that limit.

Instance ID

id-parts-updated_part2

The 4th, 5th, and 6th characters tell you the Instance or Pod that the record originated from. Salesforce expanded Instance ID from 2 characters to 3 in the Summer '23 release. Prior to that, only the 4th and 5th characters were used.

🗒️
Note

Records can be moved from one instance to another, for example, when creating or refreshing a sandbox. In fact, an entire org can be moved to a new instance. Salesforce does this from time to time to help balance their servers. Keep in mind that the Instance ID portion of a Salesforce record ID will never change. It always reflects the originating instance, which will not always be the record's current instance.

The algorithm and/or encoding logic to get from an Instance/Pod ID to an Instance/Pod Name like "NA33" or "CS55" is not publicly documented. There have been some attempts by the community to catalog this mapping but they mostly seem incomplete and potentially out of date, so use with caution. The best one I have seen so far is in this Stack Overflow post.

Personally, I think that its work documenting what this part of the ID is used for, but I don't see a lot of practical usage this part.

Reserved for Future Use

id-parts-updated_part3

This one is simple enough. Salesforce baked in a little future proofing in their ID design. At the time of writing this, the 7th character will always be zero. At some point, Salesforce may use these characters for some future purpose. Prior to the Summer '23 release, both the 6th and 7th characters were reserved for future use. See above.

Record Number

id-parts_part4

Characters 8 through 15 comprise an identifier for this particular record. It is not publicly documented but it seems to be perhaps a base-62 encoded number. My guess is that given a particular database table and originating Pod ID, this number would be unique. Or perhaps it is unique within the pod, I have not analyzed a large dataset to prove that theory, but it seems plausible. If anyone knows for sure, please leave a comment below 🙏.

Optional Suffix

id-parts_part5

The last three characters, characters 16, 17, and 18 are optional and, if present, will render an ID case insensitive.

For example, 0066A000004rMoL and 0066A000004rmoL do not represent the same record. Note that both IDs are 15 characters and theM is uppercase in the first ID and lower-case in the second.

This is important to remember when working with Salesforce records outside of Salesforce. For example, deduplication of records in a spreadsheet. If the comparison does not take into account the case, you could end up with unexpected results.

📜
A Bit of History

Prior to 2009, All Salesforce IDs were 15-characters long. Since then, Salesforce has been implementing 18-character IDs throughout the platform. Nowadays, it is very rare to find any place within the platform that will emit a 15-character ID, but they are, nonetheless, still valid.

With the 3-digit suffix, 0066A000004rMoLQAU and 0066A000004rmoLQAU do represent the same record. even though the M is still lowercase in the second ID, Salesforce knows that these IDs are equivalent because of the suffix, which in this case is QAU.

The algorithm for generating a suffix for a 15-character ID is known, and there are several online tools for generating them, including one right here on CodeByCody.com 😉

Salesforce ID Converter
Convert 15-character Salesforce IDs to 18-character IDs.

If you would like to take a deep dive into the algorithm, I've got a post about that:

Salesforce’s Algorithm for Converting IDs from 15 to 18 Characters
Need to convert 15-character Salesforce IDs to 18-character IDs. Here’s how:

Final Thoughts

I hope this has helped educate and clear up any misconceptions about Salesforce IDs. If I left anything out, or something was unclear, please let me know in the comments below. Thanks!

Share this post