Algorithm:
Saleforce uses 15-character IDs as primary keys for every record.
When retrieving an ID over the API, 18-character case-insensitive IDs are returned.
Generally, this doesn't cause any problems because the 18-character version can be used anywhere the 15-character one is.
One problem arises though when you are storing IDs in a text field.
In this case, you may need to convert a 15-character ID into its 18-character version.
All case-sensitive ids are 15 chars.
To convert a 15 char case-sensitive id to an 18 char case-safe id follow these steps.
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. Constuct an array that contains the sequence of capital letters A-Z and 0-5.
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.
I used this code as example.
- def sf15to18(sf_id15)
- map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345'
- extra = ''
- sf_id15.scan(/.{5}/).each do |chunk|
- bits = []
- chunk.scan(/.{1}/).each do |char|
- #Uppercase condition
- bits.unshift( (char.to_i == 0 && char != 0 && char.downcase != char) ? 1 : 0)
- end
- ind = bits.inject(0) do |ind, bit|
- ind + ind + bit
- end
- extra += map[ind..ind]
- end
- sf_id15 + extra
- end