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

<span class="n">library</span><span class="p">(</span><span class="n">RForcecom</span><span class="p">)</span><span class="w">
</span><span class="n">source</span><span class="p">(</span><span class="s2">"~/OneDriveBusiness/Projects/Authentications/SalesForce.R"</span><span class="p">)</span><span class="w">
</span><span class="n">session</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">rforcecom.login</span><span class="p">(</span><span class="n">username</span><span class="p">,</span><span class="w"> </span><span class="n">password</span><span class="p">,</span><span class="w"> </span><span class="n">instanceURL</span><span class="p">,</span><span class="w"> </span><span class="n">apiVersion</span><span class="p">)</span><span class="w">
</span>

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

<span class="n">objects</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">rforcecom.getObjectList</span><span class="p">(</span><span class="n">session</span><span class="p">)</span><span class="w">
</span>

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.

<span class="n">getAllFields</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">objectName</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">description</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">rforcecom.getObjectDescription</span><span class="p">(</span><span class="n">session</span><span class="p">,</span><span class="w"> </span><span class="n">objectName</span><span class="p">)</span><span class="w">
  </span><span class="n">fields</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">as.character</span><span class="p">(</span><span class="n">description</span><span class="o">$</span><span class="n">name</span><span class="p">)</span><span class="w">
  </span><span class="n">rforcecom.retrieve</span><span class="p">(</span><span class="n">session</span><span class="p">,</span><span class="w"> </span><span class="n">objectName</span><span class="p">,</span><span class="w"> </span><span class="n">fields</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span>

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

<span class="n">accounts</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">getAllFields</span><span class="p">(</span><span class="s2">"Account"</span><span class="p">)</span><span class="w">
</span>

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)