Skip to content

Instantly share code, notes, and snippets.

@xavdid
Last active March 10, 2016 21:02
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 xavdid/ad35093e5d4b2443487c to your computer and use it in GitHub Desktop.
Save xavdid/ad35093e5d4b2443487c to your computer and use it in GitHub Desktop.
Convert Salesforce 15 digit id to 18
class InvalidIdError < RuntimeError
end
def convert_to_18(id)
if id.nil?
return nil
elsif id.size != 15
puts "INVALID ID: #{id}"
raise InvalidIdError
end
suffix = ''
3.times do |i|
flags = 0
# 3 chunks of 5 chars
5.times do |j|
c = id[(i * 5) + j]
# add flag if c is uppercase
if /[[:upper:]]/.match(c)
flags += (1 << j)
end
end
suffix += ['A'..'Z', '0'..'5'].map(&:to_a).flatten[flags]
end
id + suffix
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment