The Blog - Expert Thoughts & Opinions

Converting Salesforce 15 digit Ids to 18 digit

Every now and then I need to convert a 15 digit Id into an 18 digit one. Salesforce provides functions in formulas etc. to do this conversion for you i.e. CASESAFEID({!AccountID}) and most times you extract Id’s in Apex, they are in the case safe 18 digit format, so you would rarely have a need to convert them yourself.

However, if you are ever creating Salesforce CSV uploaders or perhaps carrying out manipulation outside of Salesforce it can be handy to know how to convert from 15 to 18 digit or the reverse!

So let's start back a little on …

Why are there 2 different formats that mean the same thing?

Well, way back when, all Salesforce IDs were 15-characters and case-sensitive.
This meant that the following Ids all referenced different records in Salesforce:
• 001C000000o92OI
• 001c000000o92OI
• 001C000000o92oi
• 001C000000O92OI

It is the “case” (whether they have capital letters of not) or the Ids that make the difference here.

This did not represent an issue at first, it was not until the Id’s were extracted from Salesforce and used by other programs i.e. Excel etc. that the “issues” cropped up as some programs did not maintain the case of the ids.

For example, if using Excel and using the Excel VLOOKUP function to match up records, then the problem is that VLOOKUP doesn’t handle case-sensitive data well.
As such an Id extracted as “001C000000o92OI” and then in a third party application it was converted to lower case “001c000000o92oi” and when it was to be updated in Salesforce again, it represented a completely different record !!!

To assist in this, Salesforce came up with a Case-insensitive version of the same Id. They do this by adding three letters at the end of the id to ensure it will always be Case-insensitive.

How to convert from 15 to 18:

This is where it gets simple!
First, you look at the ID and split it into groups of 5 chars:
001C0 00000 o92OI

These 3 groups of five will be converted into the last three characters at the end of the ID
Next quite simply, for each character, give it a 1 if it is uppercase or a 0 if it is lower case

So for the Id:
001C0 00000 o92OI
00010 00000 00011

This is a binary representation of the Uppercase characters in the original Id
Now we “mirror” the binary numbers (aka swap the bits from left to right) so the digits appear as they would in a mirror

Thus:
00010 00000 00011
Becomes:
01000 00000 11000

Now we turn these binary numbers back to decimal.
01000 = 8
00000 = 0
11000 = 24

That gives us 8 0 24

Now we have to look up an uppercase Alphabet Table which “index” starts at zero.

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Looking up the table for our three numbers, we get:

8 = “I”

0 = “A”

24 = “Y”

So we have “IAY” for the end of the ID

Getting the original Id “001C000000o92OI” and adding the last three to the end of it gives us “001C000000o92OIIAY”

Now the ids below are all the same as they are case-insensitive:
• 001C000000o92OIIAY
• 001c000000o92oiiay
• 001c000000o92OIiAy

The above will always represent the same record in the same org of Salesforce.

Converting back from 18 to 15

I often read that in order to convert an 18 digit Id into a 15 digit one, you just simply cut off the last 3 characters. This will be true if the case has not changed BUT …… It will not work if the case has changed, so it cannot 100% of the time be relied on!

Most of the time this “rule” is true, however, take the instance that you extract the 18 digit Id 001C000000o92OIIAY and then after some manipulation, it gets converted into 001c000000o92oiiay.
Well if you chop off the last 3 in this situation, we know it will not represent the same record as the original Id.

If you wanted to convert the Ids back you would have to reverse the previous process and ensure the characters with a binary digit of 1 are upper-case.

Simple once you know how!

Have fun and enjoy coding!