Extracting data from Salesforce

[This article was first published on Adventures in Data, 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.

I have been asked to access and present some of our own internal data, stored on a CRM system called Salesforce. Luckily for me someone had already written a set of R bindings for it (phew). As always thank a million to the authors of RForcecom, details of which can be found here. A while ago, I was struggling to extract data from objects because you needed to know the names of the fields you wanted to return before submitting your query. Seems simple but this had me stumped for a while so I thought I would share the solution.

First we authenticate the session (I have all my keys in a seperate folder).

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

Now we can get a list of all the objects available to us.

objects <- rforcecom.getObjectList(session)

Here is the tricky bit, to query one of these objects we need to know the names of the fields we're interested in. I just wanted everything so wrote a wrapper that would pull all of the fields.

getAllFields <- function(objectName) {
  description <- rforcecom.getObjectDescription(session, objectName)
  fields <- as.character(description$name)
  rforcecom.retrieve(session, objectName, fields)

Now we can call this function and we're done.

accounts <- getAllFields("Account")

To leave a comment for the author, please follow the link and comment on their blog: Adventures in Data.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)