How the 15 to 18 character Salesforce ID conversion works, with an Excel formula (opens in a new tab)
You will learn why the 18-character Salesforce ID exists, how its three check characters are computed, and the Excel pitfall that produces a wrong suffix.
Why read this
It also shows how Apex normalizes Id values automatically.
- Split the 15-character ID into three chunks
- Set a bit for each uppercase letter
- Sum the bit values to get 0 to 31
- Map the value onto A-Z and 0-5
Salesforce record IDs come in a 15-character case-sensitive form and an 18-character form. The 15-character ID causes problems in Excel and most SQL databases, which are not case-sensitive, so VLOOKUP can silently merge records whose IDs differ only in letter case. The 18-character form appends three check characters derived from the case pattern, which makes the ID safe in case-insensitive systems.
The author describes the algorithm: the ID is split into three five-character chunks, and for each chunk a bit is set for every uppercase letter, with positions worth 1, 2, 4, 8 and 16. The resulting 0 to 31 value is then mapped onto the characters A to Z and 0 to 5. A worked example shows 00ABc producing 12, which maps to M.
The key rule for Excel is to use a formula based on CODE(), testing character codes 65 to 90, rather than comparisons made with UPPER(). UPPER treats digits as uppercase and yields a wrong suffix without any error. In Apex, developers rarely need to convert IDs by hand, since Id variables normalize automatically to the 18-character form.
The full article carries the complete Excel formula, an Apex utility method called to18CharId(), and a bulk converter that verifies its results with a checksum.
Read the original at dev.to (opens in a new tab)