Skip to content

Instantly share code, notes, and snippets.

@mikelikesbikes
Created October 29, 2012 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikelikesbikes/3976912 to your computer and use it in GitHub Desktop.
Save mikelikesbikes/3976912 to your computer and use it in GitHub Desktop.
Converts 15 character Salesforce IDs to 18 character Salesforce IDs (adds checksum)
CHECKSUM_CHARS = ((?A..?Z).to_a + (0..5).to_a).join
# String -> String
def id_with_checksum(sfid)
# protection from bad people who misuse things
return sfid if sfid.length == 18
raise ArgumentError.new("sfid must be 15 characters") unless sfid.length == 15
# The meat.
suffix = sfid.chars \ # String -> [String]
.each_slice(5) \ # [String] -> [[String]]
.map { |chars| chars.reverse.map { |c| ("A".."Z").include?(c) ? "1" : "0" }.join.to_i(2) } \ # [[String]] -> [Int]
.map { |i| CHECKSUM_CHARS[i] } \ # [Int] -> [String]
.join # [String] -> String
sfid + suffix
end
@blakesmith
Copy link

The fact that you had to write this in the first place makes Salseforce a bowl of fail. Neat though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment