Connecting R with Salesforce.com

Hey folks,

this blog post will give a short introduction on how to connect salesforce.com with R. According to Takekatsu Hiramura

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.

To connect R with salesforce.com Takekatsu Hiramura developed the R add-on package RForcecom. The source code of the package is available from github. You can install the package from CRAN:

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

To sign in to salesforce.com we can use the rforcecom.login() function. We have to set the username, password, instanceURL, apiVersion arguments as follows:

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

Please note: do not forget your security token in the password field. The Salesforce community shows how to reset the security token.

Unfortunatly we probably get an error: Error in curl::curl_fetch_memory(url, handle = handle) : Couldn’t resolve host name, because the corporate network environment uses HTTP Proxy. To pick out the proxy setting we can use the ie_proxy_info() function from the curl R add-on package.:

#install.packages("curl")
library(curl)
ie_proxy_info()

Obviously the corporate network environment uses a global proxy auto-configuration (PUC) file that defines how web browsers and other user agents can automatically choose the appropriate proxy server. We can get the info of the proxy for a specific URL using the ie_get_proxy_for_url() function:

ie_get_proxy_for_url(target_url = "https://login.salesforce.com/")

The function returns a character string with two proxies and their corresponding port. We will further use the first proxy displayed: **. Now we have to set the proxy configurations with the set_config() and use_proxy() functions from the httr R add-on package:

#install.packages("httr")
library(httr)
proxy_for_url <- "your proxy" #from ie_get_proxy_for_url(target_url = "https://login.salesforce.com/")
port_for_url  <- 8080
set_config(
  use_proxy(url = proxy_for_url", 
            port = port_for_url,
            username = "windows_username", 
            password = "windows_password", 
            auth = "ntlm")
)

Under some circumstances you will get the following error message at this point: Error in curl::curl_fetch_memory(url, handle = handle) : Problem with the SSL CA cert (path? access rights?). In this case your security certificate, e.g. curl-ca-bundle.crt is located in the wrong folder. You have to paste the file into C:/mingw64/ssl/certs.

Et voilà, we reached our goal to connect R with salesforce.com:

session <- rforcecom.login(username = username, password = password, loginURL = instanceURL, apiVersion = apiVersion)
session

Use the function rforcecom.retrieve() to retrieve a dataset:

rforcecom.retrieve(session = session, 
                   objectName = "Contact", 
                   fields = c("ID", "Name"), 
                   limit = 50)

After setting up our environment these procedures are very easy and are very useful for projects using R and Salesforce.com. Please let me know if you have any further questions or suggestions.

Andreas Kitsche
Andreas Kitsche
Data Scientist, Bonsai Enthusiast & Latin Dancer

Related