Salesforce API integration with Rails app

Salesforce is known to be a Customer Relationship Management (CRM) platform and it provides cloud-based applications for Sales, Service & Marketing. This article intends to develop a clearer understanding on the Salesforce API and how you can sync data between Salesforce and your Rails application. The following steps are required to be followed to integrate Salesforce API with your Rails application:
Use Rails gem “restforce” – https://github.com/ejholmes/restforce

Installation

Add gem ‘restforce’, ‘~> 2.5.3’ into your Gemfile Run ‘bundle install’ or ‘gem install restforce’

Configuration

In your application.yml, set the following env variables –
SALESFORCE_USERNAME: “username” SALESFORCE_PASSWORD: “password” SALESFORCE_SECURITY_TOKEN: “security token” SALESFORCE_CLIENT_ID: “client id” SALESFORCE_CLIENT_SECRET: “client secret” SALESFORCE_HOST: “host domain name” SALESFORCE_API_VERSION=”38.0″
Try to connect to salesforce run “sf = Restforce.new”

Querying to Salesforce

Salesforce can have multiple objects like- Account, Certification_c, Learner etc. To access these objects, you can apply the following queries.
  • query
  • query_all
  • select
  • search
  • create
  • find
  • update
  • destroy
1. query
Example 1:
accounts = sf.query(“select Id, Something__c from Account”)
Example 2: Time based query using WHERE clause # last synced time
last_synced_at = (Time.now.utc – 1.hour).strftime(“%Y-%m-%dT%H:%M:%SZ”)
#current system time
current_time = Time.now.utc.strftime(“%Y-%m-%dT%H:%M:%SZ”)
sf.query(“select Id, Something__c from Account WHERE LastModifiedDate >= #{last_synced_at} AND LastModifiedDate <= #{current_time}”)
2. query_all
query_all lets you to include the results from your query that Salesforce hides in the default “query” method. These also include soft-deleted records and archived records (e.g. Task and Event records, usually archived automatically after they get a year old)
accounts = sf.query_all(“select Id, Something__c from Account where isDeleted = true”)
3. select
select allows the fetching of a specific list of fields from a single object. It requires an external_id lookup, but is often much faster than an arbitrary query.
sf.select(‘Account’, ‘001D000000INjVe’, [“Id”], ‘Some_External_Id_Field__c’)
4. Search
# Find all occurrences of ‘bar’
sf.search(‘FIND {bar}’)
5. create
# Add a new Certification_c
sf.create(‘Certification_c’, Certification_type_c: ‘CSR In-Person’)
6. find
# Find a certification by id
sf.find(‘Certification_c’, ‘001D000000INjVe’)
7. update
# Update the ‘Certification_c’ with `Id` ‘0016000000MRatd’
sf.update(‘Certification_c’, Id: ‘0016000000MRatd’, Certification_type_c: ‘CSR Vertual’)
8. destroy
# Destroy a certification by id
sf.destroy(‘Certification_c’, ‘001D000000INjVe’)

API Limits

Salesforce has a limit of API call per/day/hour. So, you can simply check your Total API calls and available calls.
limits = sf.limits limits[“DailyApiRequests”]
Output = {“Max”=>15000, “Remaining”=>14746}
Rescue from Restforce API connection errors
def self.connect_to_salesforce
   sf = nil
   begin
      sf = Restforce.new
   rescue Exception => e
      error_message = "Failed to connect to Salesforce using restforce gem!"       
    end
 end

Subscribe For Latest Updates

Related Posts

About Post Author

Leave a Comment

Your email address will not be published. Required fields are marked *