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

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))

rforcecom-02

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)

rforcecom-03
rforcecom-04

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)

rforcecom-05

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)

rforcecom-08
rforcecom-07

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

rforcecom.getServerTimestamp(session)

rforcecom-06

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

  • RForcecom
    • Data Science and Software Engineering

Posted in R, Salesforce.com
21 comments on “RForcecom – An R package provides the connection between R and Salesforce.com
  1. Nguyen says:

    Hello Takekatsu,

    You had a great idea to write this package that link Salesforce to R.

    I tried the package at work but only a error message echoed back : error in function, couldn’t connect to host. It seemed that the package RCurl couldn’t reach salesforce.com . I don’t know exactly the reason so can’t report to the IT Department to ask for a fix.

    Have you ever met a similar situation ?

    Good day,

  2. Nguyen says:

    Thank you for answer. What is strange is that I don’t have any problem connect to cran server to download or update packages. I continue to search for that.

    Good day to you

    • Please let me know If you know, does your network require HTTP Proxy?

      Thanks,

      • Nguyen says:

        Yes, I think so. Moreover, the network use a proxy auto-config file. I hope that i had answer to your question. 🙂

      • I found that an external library of RForcecom RCurl has its own proxy settings. I’m making the patch for the issue.
        And if you are able to try, please use another network doesn’t require HTTP proxy.

        Thank you.

      • Nguyen says:

        Good afternoon Takekatsu,

        I could test the package RCurl without any problem at home. Concerning the Salesforce account, it is configurated only to be accessible from my company’s computers (ID restricted).
        I think if I can make Rcurl work from my company’s pc, RForcecom will do also.

        A good day to you,

  3. Nguyen says:

    Thank you very much Takekatsu.

    I will test the package at home and tell you the result tomorrow. Indeed, I think that the problem comes from RCurl since I tested the function getUrl{RCurl} and got the same message :”Couldn’t connect to host”. On contrary, the function readLines{base} and install.packages{base} work fine.

    See you

    PS: I use RStudio

  4. Aaron says:

    Very helpful – thank you! I experienced the same error connecting to the host, but realized that the “r” is missing from “Salesforce” in the instanceURL in step 3 (I initially copied the code to test). It works and I love not having to download reports from Salesforce to analyze in R!

  5. Ankur says:

    My company is currently using Digital Badges to login into SFDC and we currently don’t use our email id and OATH Tokens to login. Does this package support that ? When I tried using the normal procedure of putting in my email address and password+pin it’s not working. Invalid login is the error that shows up.

    Any ideas or suggestions. I am currently using SFDC Unlimited edition with R 3.0.2

  6. Roman says:

    That package is great!!! You can forget about nightmares with complicated ETLs. Thank you Takekatsu.

  7. DJ says:

    Hi I’m unable to connect
    Error in function (type, msg, asError = TRUE) :
    Failed to connect to na14.salesforce.com port 443: Timed out

  8. Greg says:

    I’m interested in whether data being transmitted from SFDC to R using RForce library is secure or not. Would you know how the data is transferred over the internet. Is SSL used or is data transmitted via Clear Text? Thanks for any assistance you can provide.

  9. Marco says:

    Dear Takekatsu,

    Many thanks for creating this package. It is very user friendly and easy to use…congrats :)!!!

    I wanted to ask regarding an aspect I am finding on special characters. I am encountering that when in some fields there are characters such as “№” (different from “N°”) or “Ș”, the field seems to load as NA (missing value).

    Have you found this issue before? If yes, how have you fixed it?

    Thanks a lot in advance for this.

    Regards,

    Marco

  10. Hello Takekatsu,

    Thank you so much for creating RForcecom! I use this package extensively to analyze the data that our nonprofit houses in Salesforce.

    Recently, our organization upgraded from TLS 1.0 to TLS 1.1 in Salesforce. Ever since then, I’ve been getting the following error message when I use the rforcecom.login() function:

    UNSUPPORTED_CLIENT: TLS 1.0 has been disabled in this organization. Please use TLS 1.1 or higher when connecting to Salesforce using https.

    Our org has a lot of very sensitive data that we send in and out of our Salesforce instance, so we would not want to disable the TLS 1.1 requirement. Is there a version of RForcecom that complies with TLS 1.1?

    Thanks!
    Aparna

Leave a reply to Aaron Cancel reply