Salesforce on Rails with the Databasedotcom Gem
At Highgroove, we’re always looking for new Ruby gems to help speed up development and keep the code DRY. The gem I found this time was Heroku’s very own Databasedotcom gem, a fantastic Salesforce.com API wrapper for Ruby! I must admit, I wasn’t very surprised to find a gem since Salesforce owns Heroku, but I did not expect it to be so fantastic and easy to use. Here’s how!
There are only a few steps involved in getting the Databasedotcom gem connected to your salesforce instance and working:
- Create a new Databasedotcom::Client instance –
client = Databasedotcom::Client.new(:client_id => ENV['SALESFORCE_KEY'], :client_secret => ENV['SALESFORCE_SECRET'])
- Authenticate with Salesforce –
client.authenticate :token => SALESFORCE_OAUTH2_TOKEN, :instance_url => INSTANCE_URL, :refresh_token => SALESFORCE_REFRESH_TOKEN
- Create a Ruby Class from a Salesforce Object –
- Use the newly available Ruby class named ‘Contact’ and use ActiveRecord::Base methods on it. The gem will translate the ActiveRecord syntax into SOQL and API calls for you just like you’re used to in Rails with any other database adapter!
- For example:
Contact.find_or_create_by_FirstName_and_LastName(:FirstName => 'Daniel', :LastName => 'Rice')
There are many other ways to use this gem, and its possible to interface with custom objects and custom fields, but I wanted to mainly illustrate how few lines of code it takes to make a Salesforce integration thanks to the awesome job by the Heroku team.
To see more use cases, features, and general information about this gem, please check out the Databasedotcom gem’s README on Github.
Image Credit: Salesforce.com *No Software logo is a registered trademark of Salesforce.com