Site icon R-bloggers

RForcecom – An R package provides the connection between R and Salesforce.com

[This article was first published on Data science & Software development » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In this post, I’ll introduce an R package RForcecom and its usage. As you may know, R statistical computing environment is the most populous statistical computing software, and Salesforce.com is the world’s most innovative cloud-computing based SaaS (Software-as-a-Service) CRM package.

RForcecom enables you to connect to Salesforce.com from R. It is provided as an add-on package of R and its source code are available at github.

1. Install the latest version of R
You can download the latest R statistical computing environment from the R-Project website.
http://cran.r-project.org/

2. Install and load the RForcecom
Type the commands from your R console to install and load the RForcecom.

install.packages("RForcecom")
library(RForcecom)

3. Sign in to Force.com or Salesforce.com
To sign in to the Salesforce.com, use rforcecom.login() function. Set your username, password, instance URL, API version as follows.
Note: DO NOT FORGET your security token in password field.

username <- "yourname@yourcompany.com"
password <- "YourPasswordSECURITY_TOKEN"
instanceURL <- "https://na14.salesforce.com/"
apiVersion <- "26.0"
(session <- rforcecom.login(username, password, instanceURL, apiVersion))

4. Retrieving records
To retrieve the dataset, use rforcecom.retrieve() function. Set parameters as follows.

objectName <- "Account"
fields <- c("Id", "Name", "Phone")
rforcecom.retrieve(session, objectName, fields)


5. Execute a SOQL
To retrieve the dataset using SOQL (Salesforce Object Query Language), use rforcecom.query() function. Set parameters as follows.

soqlQuery <- "SELECT Id, Name, Phone FROM Account WHERE AnnualRevenue > 50000 LIMIT 5"
rforcecom.query(session, soqlQuery)

6. Create a record
To Create a record, use rforcecom.insert() function.

objectName <- "Account"
fields <- c(Name="R Analytics Service Ltd", Phone="5555-5555-5555")
rforcecom.create(session, objectName, fields)


7. Retrieve a server timestamp
To retrieve a server timestamp from Salesforce.com server, use rforcecom.getServerTimestamp() function.

rforcecom.getServerTimestamp(session)

These procedures are very easy and are very useful for projects using R and Salesforce.com. Next post, I’ll introduce an example of a use case of RForcecom.

RForcecom website