All Posts By

Seth Davis

How to Integrate the Salesforce API into your Rails Project

By DevelopmentOne Comment

Many companies use Salesforce, it’s one of the biggest Customer Relationship Managers (CRMs) available right now. Many companies use Rails as well. Combining the power of Rails with the power of Salesforce can greatly benefit any company. Where do you begin with this process? Let us look into a (not so little) Ruby gem, named Restforce.

Restforce

 

Restforce is a Ruby gem made for the Salesforce REST API. It includes many features that help developers interact with the Salesforce API using REST, and has been downloaded over 9,000,000 times. Installation is simple, just like any Ruby gem, just write

gem ‘restforce’

Into your Gemfile, and then

bundle install

Into your terminal.

Initialization

 

Before you begin sending requests to your Salesforce org, you’ll need to first initialize and authenticate your client inside a model as a method, so you can connect properly. There are many ways to do this, but here, I am going to use the username/password authentication method.

Here we are setting the client. There are many variables involved to get a proper connection, so I will go over each of them.

  • Username and password are, of course, the account’s username and password.
  • The security token you can get by requesting one from Salesforce. In order to see your token, you must go to SETUP > MY PERSONAL INFORMATION > RESET MY SECURITY TOKEN. From here you can simply press the button to get an email from Salesforce, giving you a new security token. Note, that the only way to view your security token, is to change it. So save the token after receiving it, in order to avoid hassle later. Changing the security token will also lead Restforce to return authentication errors until the security token is updated in the codebase or environment variables.
  • To view your client ID and your client secret ID, you must go to SETUP > APPS > CONNECTED APPS > MANAGE CONNECTED APPS. From there you should see a list of apps you have connected. Click on one to see all its information, including the client ID and the client secret ID located in the OAuth section.
  • API version is exactly what is sounds like. It is normally best practice to make this the latest version of the Salesforce API.

It is also a good practice to add the keys, passwords, etc into environment variables. Having access to these keys means you have access to any records and fields in Salesforce! Now that everything is set, let’s begin interacting with the API.

Requests

 

Requests are the main way to interact with any REST API. Restforce makes it incredibly easy to make any request to the Salesforce API.

  • As any software developer knows, GET requests are when you need to pull data from an API. This can be done a number of ways through Restforce. Here are a few of the most common ways to get data.

  • POST requests are the next most important. These let you send information to the Salesforce API.

  • PATCH requests are like POST requests, in that you send information to the Salesforce API. However, this time you are changing an object, rather than creating a new one.

  • Finally DELETE requests will remove objects from the Salesforce API.

With these methods at your disposal, you are ready to integrate the power of the Salesforce API into your Rails project.

Example

 

Model

Controller

You can find the Github repository here.

API Call Limit

 

Note that each Restforce call you make is an API call, each Salesforce account is given a certain amount of API calls per month (depending on your Salesforce contract). Be sure to check your API usage. If you’re interested in updating a massive number of records, then perhaps use dataloader.io or the application “wizard”, where you can upload a csv to massively create, delete, and update records. You can read more about that on our blog here.

Data Sync

 

While being able to connect your Rails application to your organization’s Salesforce account can be an empowering tool, there is the risk having data sync issues if you’re storing records in your own database in addition to creating and updating records in Salesforce. It’s important to note that when you create Salesforce records, the record could change in Salesforce, and your Rails application won’t even know about it. So when using this gem, one thing to think about is where your source of truth is if you’re saving and updating in multiple places. Saving to your own database is still a good practice in case for some reason there is an outage out of your control on the Salesforce side of things.

Conclusion

 

Now you know how to integrate your Salesforce org and your Rails app. Using the Restforce gem, you can easily connect to your org. Making REST requests is also extremely simple. Simple methods allow easy GET, POST, PATCH, and DELETE requests to be made. Now that Rails and Salesforce are connected, interacting with Salesforce through your own app has never been easier.

How to Import Your Rails Data Into Salesforce With CSV

By DatabasesOne Comment

Oftentimes, companies want to move resources into Salesforce. It only makes sense, being that salesforce is one of the top Customer Relationship Managers (CRMs) available right now. There are loads of benefits to storing data in Salesforce, including security, organization, and data recovery. What is the process of moving data to Salesforce like? Let’s talk about CSVs.

Process

 

  • When working in Rails, all your data is stored in a database. First you’ll need to pull the required data from the database and put it inside a CSV file, using the psql command line. CSV stands for comma-separated values, and is a way of storing table-like data in a text format.

user$ psql
user=# \c database
database=# \COPY my_table TO 'filename.csv' CSV HEADER

This will create a CSV file in the chosen directory with the contents of the table from your database.

  • From here, we will begin to upload our CSV file to Salesforce. First you’ll need to go to SETUP > DATA > DATA IMPORT WIZARD. Where you can find and launch the wizard to import data.
  • Once the wizard is launched you will be asked to choose what type of data you are importing. This is basically asking which Salesforce object you are looking to import, whether it be contacts, leads, or a custom object you created. Select which type you are planning to import.

  • Next you will be asked what you want to do. Since we’re importing new data in this tutorial, we will select “Add new records”

  • After that, select the CSV file you would like to import, select the correct Character Code, and locate the option by which the values are separated by. The character code and separator, will more often than not be the default values Salesforce sets. After this step, then just press “Next” at the bottom right of your screen.

  • After pressing “Next”, you will be directed to a new page asking to assign Salesforce objects to the CSV headers.

Some headers will be mapped already, as Salesforce typically knows what some values will be mapped to, saving you time. However in this situation, our contact_name and acctount_name headers are not mapped, so we will have to do it manually, starting by clicking “map” on the left side of the screen.

  • Mapping an object to a header is quite simple, as all you need to do from the pop-up box, is find the related field, or fields, and select them to map.

Now after repeating that for each un-mapped header, you are ready to begin importing.

  • Click the “Next” button at the bottom right corner of the screen for a review of what is being imported. From there, just press “Start Import” to begin putting your data into Salesforce. Note: This can take some time depending on how much data you have.
  • After the import you should now have a list of objects available to you on your Salesforce homepage.

Conclusion

 

Now that you know how to import your data, you have the power of Salesforce on your side. You can create graphs, organize your data, as well as many other ways to improve your business. Data can also be updated and new data can be added at any time, so you can always have Salesforce up to date with the rest of your company. If you are looking to centralize your data in either Salesforce or Rails, rather than both, look here to help you make a decision.

Should You Build Your App From Scratch or Using Salesforce?

By Development, DjangoOne Comment

An important decision that comes to all companies before building out an application, is the decision to use either an app builder like Salesforce, where almost no prior coding experience is required, or a web development framework, like Ruby on Rails, C#, Java, or Django (we will simply refer to rails for now). It’s an important decision that will determine the development path to follow. In this article, I’m going to go over the benefits of both Salesforce and Ruby on Rails in order to help you make an educated decision on what path you would like to follow.

Salesforce

 

Pros:

Streamlined – Salesforce strives to be an end to end software data tool to manage your complete enterprise. You can easily connect all your employees using streamlined tools and applications that come out of the box. This requires fewer resources overall and allows you to manage your business from anywhere.

Mobile First – Salesforce also gives companies and developers an easy way to create your mobile applications. In fact, it’s touted as being built mobile-first. The instant you create an app for your browser, a clean-looking mobile version is generated that can be used by all users.

Simple – As you may be able to tell from the previous benefits of Salesforce, it is built to be straight forward. Salesforce is proud to note that their software can be managed by others who have have less technical experience. This is especially true due to Salesforce’s own Trailhead, which is “The fun way to learn Salesforce”.

Cons:

Limited Customization – There isn’t much customization for how Salesforce apps work and look.  If you purchase a solution that is built on top of Salesforce, you are often limited in your ability to customize how it will work as you now rely on Salesforce updates. For users on Salesforce, some functionality may break after Salesforce update. Updates usually consist of large layout changes to the platform, causing problems to the businesses using it by making some important features now hard to find. This also gives new Salesforce businesses a learning curve around the Salesforce system, not the business.

Expensive – While Salesforce seems inexpensive out of the gate, costs can quickly escalate out of control.  If your ROI (return on investment) isn’t there, Salesforce will be an anchor instead of a rocket boost.  Many times, the rising costs of additional but much needed capability causes subscription creep that the company is saddled with.

Developers Needed – Many companies try and wedge in a java or .net programmer and tell them to code up Salesforce apps or integration only to find that they cannot do the work. To build and operating in Salesforce at a deeper technical level, you need to have experiences certified Salesforce personnel on your team.

Building From Scratch – Ruby on Rails/Django

 

Pros:

Application Flexibility – If you are building a specialized software product, rails is also no doubt one of the best for designing a customized platform.  Rails allows you to customize everything from design, to workflow, to api exposure. In Rails, because developers practically start from the ground up, you can put a very personal touch on your app.

Platform Flexibility – There are no platform limitations.  If you can think of it, you can build it.  You are not limited to restrictions. Building your apps in Rails will also save you from worrying about third parties. With Rails, the only business you need to rely on is your own.

Cost – For smaller companies with developer experience looking to start up a new application can find Rails applications to be more affordable with server usage being the only minimal cost.

Cons:

Experienced Developers – Working on a Ruby on Rails web-app will require one or multiple experienced developers. Connecting your users is also a greater hassle as well, compared to Salesforce.

 

Conclusion

If you are looking for a flexible and cheap way to create a CRM, then building your application from scratch would be your best choice! Especially if you are looking to create a much more personalized CRM that works your way. On the other hand, if you are looking for something simple and streamlined, Salesforce is the choice for you! As anyone can learn Salesforce! In the end, it’s whatever benefits your business the most, for whatever its needs may be. You could even do both, with the ability to integrate the Salesforce API into your Rails project.

GIS Crawling Done Easy Using Python With PostGreSQL

By AWS, Databases, Development, GIS, Python, RDSNo Comments

Problem

Company “Help-i-copter” allows users to rent a ride in a helicopter for a lift in style. However, Help-i-copter is having trouble creating software that find the best flat surface for the helicopter pilots to land to pick up their customers. A perfect solution would be crawling data from sites and to store it on a PostGreSQL database. What steps should they take?

Crawling

Crawling, in software terms, is the act of copying data from a website using a computer program. This data can be saved to a file, printed to your screen, or put into a database. It entirely depends on the project and its scale. On a much greater scale, where data can be overwhelming, a database would be the best option, which is why it’s important to have one. You could have your grocery list stored online, but you don’t want to login every time to see it. You could instead crawl that information off the web with a command like:

soup.find_all('a', attrs={'class': ‘list'})

  • Write a crawling program that collects data from multiple sources, to get the most accurate data. This program would most likely be written in Python as it has access to modules like Requests and BeautifulSoup.

import requests
from bs4 import BeautifulSoup
url = "http://www.placeforyourhelicopter.com"
r = requests.get(url)
soup = BeautifulSoup(r.content, "html.parser")
surfaces = soup.find_all('a', attrs={'class': 'surface'})
for d in surfaces:
#getting all the surface information

  • Store the found data on a PostGreSQL database. This allows the data to be stored and sorted. When it’s sorted, this allows the data to be queried quickly and efficiently.

import psycopg2
endpoint = "surfaces.amazonaws.com"
c = psycopg2.connect(host=endpoint ,database="surfaces", user="admin", password="admin")
cursor = c.cursor()
query = "INSERT INTO surfaces (airtraffic,windspeed,date_produced,lat,lon,state) VALUES (%s,%s,%s,%s,%s,%s);"
data = #all info from surfaces in an array
cursor.execute(query, data)

  • Create an app that allows pilots to enter in a variable they want to query and parameters for that variable. Then use Python and the Psycopg2 module to query the data accordingly. This app with allow non-programmers to access the database without having to learn PostGreSQL.

letter = raw_input("Choose a number: ")
cursor.execute("SELECT * FROM surfaces WHERE airtraffic LIKE " + letter)
for row in cursor:
print str(row) + "\n"

 

Databases

So why is it important to store large amounts of data on a database? Simply, it gives you an easy way to find and query data. For example, let’s say you have a list of employees you got from your company’s website, and added it to a PostGreSQL database. In such a database, finding data like “all employees with a first name that begins with an L” would be much simpler, as databases are well organized. Simple commands like:

where employee_name LIKE 'L%'

would return “Larry, Liz, and Lucy” quickly and efficiently.

Bounding Box

 

This data could be used in a lot of ways. Namely, we could use out latitude and longitude coordinates to create a bounding box and get information about the areas within that box. This could help Help-i-copter in a number of ways. Pilots could use a bounding box to find flat areas near them and sort those by air-traffic, location, etc… It will be essentially asking the pilot the maximum and minimum coordinates of the box and then looking through the database for any surface that fit the description. Here’s what that might look like in Python:

xmin = raw_input("What is the minimum latitude? ")
xmax = raw_input("What is the maximum latitude? ")
ymin = raw_input("What is the minimum longitude? ")
ymax = raw_input("What is the maximum longitude? ")
cursor.execute("SELECT * FROM surfaces WHERE GEOMETRY(lat,lon) && ST_MakeEnvelope("
+ ymin + "," + ymin + "," + xmax + "," + ymax + ", 4326)")
for row in cursor:
print str(row) + "\n"

Conclusion

 

As you can see, crawling and databases can work very well together, especially when crawling large amounts of data. It would be otherwise inefficient and a lot slower to just store data on a normal document, or to do a crawl of the page every time you run the program. Help-i-copter able to efficiently crawl a site, upload data to a web page, and query that data back to a pilot quickly. Thanks to the power of PostGreSQL and Python.