query Salesforce with R

How to connect to the Salesforce data with R

Here is how to get necessary Salesforce data with R. Previously, there was a package RForcecom, but now you should use the salesforcer package instead. During this year, RForcecom was removed from CRAN.

The first task is getting all the necessary Salesforce authorization information, and the rest is easy.

You will need your Salesforce username, password, and security token that you can find in your Salesforce profile settings section Reset My Security Token (Profile icon > Settings > Reset My Security Token).

Query some of the Salesforce data with R

Here is how to query some of the Salesforce data with R. When using sf_auth for the first time, you have to allow R access. A dedicated web page will open for authentication to complete.

require(salesforcer)

user <- 'myusername'
password <- 'mypassword'
token <- 'thisisalongtoken'

password.token <- paste(password, token, sep = "")
session <- sf_auth(user, password.token)



soqlQuery <- "SELECT Id, Name
              FROM User"

users <- sf_query(soqlQuery)

It is almost the same as with the previously used package RForcecom, but it is recommended to use salesforcer functions. Functions from RForcecom will work, but there will be a warning that these functions are deprecated.

It is not the best practice to store your credentials in R script. If you are a Windows user, then try to store your password and token securely in R.

 

For the Salesforce CRM data exploration, I found useful Power Query that is built-in Excel or Power BI. Here is a post that explains how to connect to Salesforce by using Excel.

 

salesforcer sf_query error in .x[[i]] : subscript out of bounds

My best guess is that this error is a result of the wrong API type.

Function sf_query has a parameter api_type that by default is specified as REST. Change that to SOAP or another type and see if it helps.

 





Posted

in

Comments

Leave a Reply

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