Salesforce originally used case-sensitive 15-character alphanumeric IDs. Later on, added three error-correcting characters to make their 18-character ID.



  • Converts a 15-char String to its equivalent 18-char ID as a String: SalesforceIdFormatter.to_18('70130000001tcyI') # => '70130000001tcyIAAQ'`
  • Leaves a 18-char String unaltered: SalesforceIdFormatter.to_18('70130000001tcyIAAQ') # => '70130000001tcyIAAQ'
  • Raises SalesforceIdFormatter::InvalidId if the given ID doesn't follow Salesforce's conventions TL;DR: 15 or 18 alphanumeric, case-sensitive chars

SalesforceIdFormatter.to_15 applies the same rules, returning 15-char IDs instead.

Code is a modified version of (original does seemt to calculate control digits correctly)


Add this line to your application's Gemfile:

gem 'salesforce_id_formatter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install salesforce_id_formatter


After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to


  1. Fork it ([my-github-username]/salesforce_id_formatter/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request