You are currently browsing the tag archive for the ‘gem’ tag.
In this tutorial we will go from zero to ninja speed in approximately ten minutes. If you have problems approaching ninja speed, it is probably because you are in ruby version management hell, gem dependency hell, or any other number of hells that brave would-be ninjas are occasionally trapped in. Should you end up here, prayer and Google may help you.
Step one: Create your rails app.
rails new ninjaspeed
Step two: Add the database.com and helper gem to your GEMFILE (this will be in the ninjaspeed directory)
gem 'databasedotcom' gem 'databasedotcom-rails'
Also add the postgres gem (you aren’t using the db on Heroku but need it anyways) and a gem to handle precompiled javascript (for the JQuery Mobile):
gem 'pg' gem 'therubyracer'
Step three: Install the new gems
sudo bundle install
Step four: Add scaffolding for a new resource that corresponds to a resource in Salesforce
rails generate scaffold_controller Lead FirstName:String LastName:String Company:String
Add an entry in your routes.rb file
resources :leads
Rails is straight up MVC, and you just created a controller and a view. Check out your app/controllers and app/views folders to see them. Salesforce/Database.com will be your Model.
Step five: Deploy this baby to Heroku
git init git add . git commit -m "i love databasedotcom" heroku create git push heroku master heroku addons add:piggyback_ssl
Step six: Take the new funky fantastic name you got and enable remote access in Salesforce
Login to salesforce. Go to Setup -> Develop -> Remote Access -> New Enter the address of your app (e.g. https://stormin'-samurai-558.herokuapp.com) You will get a client key (this is also the client_id) and your client secret
Step seven: Add your salesforce credentials to a file in the config folder called databasedotcom.yml
databasedotcom.yml #### client_id: 3MVG..... client_secret: 1323224123... username: mysalesforceusername@login.com password: passwordPlusSecToken host: login.salesforce.com <-- use test.salesforcecom if using a sandbox debugging: true
Step eight: Connect your app to Salesforce
Add this to your app/controllers/leads_controller
LeadsController < ApplicationController include Databasedotcom::Rails::Controller
In the same file you need to add a few more required fields. Navigate to the create method and add three lines:
def create
@lead = Lead.new(params[:lead])
# ADD ME
@lead['OwnerId'] = '005U0000000IekIIAS' <-- ownerId here is the Id of the User you want the Lead associated with
@lead['IsConverted'] = false
@lead['IsUnreadByOwner'] = true
# END
Thanks to some backend magic, the databasedotcom-rails gem automatically initializes the client you need to connect to salesforce so you don’t have to worry about it.
Step eight: Add in a jQuery mobile stylesheet so this doesn’t look super ghetto (optional)
Navigate to app/views/layouts/application.html.erb and edit these lines to the header:
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" /> <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script> <script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
Put the yield tag in the body in a div like so:
<div data-role="page"> <%= yield %> </div>
For your apps/views/leads/new.html.erb, divide into a couple of sections
<div data-role="header"> Lead form </div> <div data-role="content"> <%= render 'form' %> </div> <div data-role="footer"> If you want it </div>
Navigate to config/environments/production.rb, find the config.assets.compile, and set it to true (turn on the live compiliation of assets)
config.assets.compile = true
Step nine: Remove extra stuff from your _form.html.erb/strong
This is all you need:
<%= form_for(@lead) do |f| %>
<div class="field">
<%= f.label :FirstName %><br />
<%= f.text_field :FirstName %>
</div>
<div class="field">
<%= f.label :LastName %><br />
<%= f.text_field :LastName %>
</div>
<div class="field">
<%= f.label :Company %><br />
<%= f.text_field :Company %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Step ten: Deploy to Heroku
git add . git commit -m "I love databasedotcom even more now" git push heroku master
Navigate to yourapp.heroku.com/lead/new, and presto-chango, a mobile-enabled submission form that hooks up to Salesforce.
You can also view leads in the system by navigating to yourapp.heroku.com/leads
Also, if you want to add in validation (and I suggest it) you can do this client-side rather than serverside.
Oh, and in case you were wondering the source code for all this is available on Github.
What follows are instructions on how to get started with the databasedotcom gem, released yesterday afternoon at Dreamforce.
The examples here will be assuming you use Heroku for hosting and deployment although you can use all of the features mentioned without Heroku if you so chose.
This is also for the most part a tutorial and not a replacement for the existing documentation includes as part of the databasedotcom gem on Github and shared via Ruby docs, which hopefully will be expanded as the gem gets some love.
There are two main components of the current release:
(1) A wrapper for the Force.com REST API which currently works with both Force.com and Database.com
(2) A wrapper for the Chatter API which will not be useful until Winter ’12 (or in orgs w/ api v. 23 already enabled)
There is also an additional controller released separately on Github which provides a class that your controllers (assuming you are using a Rails app) can extend. By doing so they will instantly get access to the Force.com or Database.com backend you are hooking into.
As of right now, the gem only supports Ruby 1.9.x, although support for earlier versions of Ruby is promised within the very near future. Although the development of the gem was commissioned by Salesforce/Heroku, maintenance is less certain and depends largely on your contributions.
The first thing you need to do is include the gem in your app. Add the databasedotcom gem to your Gemfile.
gem 'databasedotcom'
If desired, use the edge version instead of the released gem version. This will allow you to get the latest version (you will probably want this if testing in the next couple of days, not necessarily later once the few warts have been ironed out).
gem 'databasedotcom', :git => "https://github.com/heroku/databasedotcom.git"
I’m assuming you’ve already initialized your app on Heroku. If not, go through the Heroku starter tutorial and create your heroku app.
I also recommend adding ssl to your Heroku app:
heroku addons:add ssl:piggyback
The first thing you need to do is add your credentials. You will need to add remote access for your heroku app in Salesforce (Setup -> Develop -> Remote Access).
Note your consumer key and secret. For the purposes of this gem, the client id is always your consumer key, and client secret is the same as consumer secret.
You can add this information to your app in one of three ways: (1) explicitly passing a hash. (2) Setting environment variables via Heroku (3) setting up a yml file in your config folder with the credentials. I recommend doing (3) unless you have a reason to use one of the other options.
The second thing you need to do to authenticate is do the OAuth handshake.
I recommend using the username and password combination for the user with the appropriate permissions for your org.
These can also be set in your yml file. I recommend also turning on debugging.
This will lead to a file called “databasedotcom.yml” in the config folder with the following info:
client_id: CONSUMER_KEY_FROM_REMOTE_ACCESS
client_secret: SECRET_FROM_REMOTE_ACCESS
username: SALESFORCE_USERNAME
password: PASSWORD_PLUS_SEC_TOKEN
debugging: true
Now make sure that is included and deploy the yml to Heroku.
To actually use the gem I recommend starting by extending the databasedotcom controller. Your code will look like this:
class SObjectNameController < DatabasedotcomController
You can then easily refer to the objects you need in that controller.
For instance in the index method you can do
@users = User.all
Where “User” is now your automagically instantiated SObject class queried via the REST API. The way this works is that if you use a variable (e.g. “User”) that is unknown to the class it will find all of the available SObjects and attempt to instantiate a new instance.
That should be enough to get you started. A bunch of other useful examples are in dburkes’ example code.
One note of caution, there is as of yet no session management for this app, so if your auth token times out you will need to re-authenticate yourself.
