Convert 15-char to 18-char IDs in Apex
I was working with someone recently who saw how I converted 15 character IDs to 18 character IDs in Apex and thought it was clever. I always thought it was something everyone knew, but just in case, I’m posting it.
All I am doing is using the ID datatype, which does it for you automatically. You just have to trap the StringException in case you get passed a string that isn’t an ID. Anyone else do it another way?
string s = '0013000000K7WW2'; try{ ID sID = s; // if we get here, it's a valid ID and the sID field is an 18 character one } catch (System.StringException e){ // if it goes here, it's not an ID. Do something if you need to. }
Prasanna Said,
August 23, 2010 @ 8:17 pm
Awsome! Many people like me must not be knowing this… Thanks for sharing this.
Jon Mountjoy Said,
August 23, 2010 @ 11:32 pm
Oh very nice!
Francis Said,
August 24, 2010 @ 3:47 am
Cool! On a side note I’ve been hunting for ages for the formula to convert the 15 and 18 char IDs. I know I watched how to do it in the iTunes training videos but have no idea which one… you don’t happen to know what it is?
Scott Hemmeter Said,
August 24, 2010 @ 7:49 am
@francis. sorry, no idea.
Jonathan Keener Said,
August 24, 2010 @ 8:06 am
Below is an excel VBA function with the logic in it. It’s always worked well for me. I got it out the the old Excel Connector code.
‘
‘ Converts a 15 character ID to an 18 character, case-insensitive one …
‘ got this one from sforce community
‘ thanks go to Scot Stoney
‘
Function FixID(InID As String) As String
If Len(InID) = 18 Then
FixID = InID
Exit Function
End If
Dim InChars As String, InI As Long, InUpper As String
Dim InCnt As Integer
InChars = “ABCDEFGHIJKLMNOPQRSTUVWXYZ012345”
InUpper = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”
InCnt = 0
For InI = 15 To 1 Step -1
InCnt = 2 * InCnt + Sgn(InStr(1, InUpper, Mid(InID, InI, 1), vbBinaryCompare))
If InI Mod 5 = 1 Then
FixID = Mid(InChars, InCnt + 1, 1) + FixID
InCnt = 0
End If
Next InI
FixID = InID + FixID
End Function
Rhonda Ross Said,
August 24, 2010 @ 10:54 pm
@francis
Not sure if this is what you saw, but if it was a video about using Excel Connector with Salesforce, the function is FixID().
As an example, if you have a 15 character ID is cell A1, if you put the formula FixID(A1) into cell B1 the 18 character ID would be displayed in cell B1.
Joey Chan Said,
September 2, 2010 @ 7:56 pm
You could also check out this snipplr link on a way to convert 15 char to 18 char using Apex.
http://www.snipplr.com/view/39952/salesforce-convert-15char-to-18char-ids-in-apex/
It came from the apex-lang package
Rodrigo Juliani Said,
August 2, 2011 @ 6:38 am
Thanks for the tip. It really saved my life.
km Said,
March 22, 2012 @ 9:51 am
This is a very nice post, i was doing it by looping over the digits and with all the logic in the code to convert it to 18 digit. This was so simple all the time…feel like a dummy
askmrlee Said,
August 27, 2012 @ 3:17 pm
I have Excel Connector installed, and I can convert 15 character IDs to 18 character IDs using =FIXID(cell reference). However, I have a column with almost 100,000 rows and =FIXID seems to work only for cells lower than row 65,536. Anything above returns a #VALUE error. I’m using Excel 2007. Any workaround other than splitting the list into two?