Created
October 29, 2012 22:18
-
-
Save mikelikesbikes/3976912 to your computer and use it in GitHub Desktop.
Converts 15 character Salesforce IDs to 18 character Salesforce IDs (adds checksum)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The fact that you had to write this in the first place makes Salseforce a bowl of fail. Neat though!