Compare Dates With RForcecom

November 25, 2016

[This article was first published on Dan Thompson, 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.

Have you used RForcecom to connect R to Salesforce? It’s really cool, and thanks to it using Bulk API, it’s absolutely rapid.

Anyway, I recently came across a problem when trying to query records which had been edited based on the timestamp.

I wanted to do something like this:

q <- paste0("select Id from opportunity where LastModifiedDate > ",time_now)

However, the Salesforce time format does not appear to match any of the POSIXlt or POSIXct formats in R (please correct me if I’m wrong!).The problem appears to be that R datetimes are generally similar to this:

“2016-11-12 12:15:32 GMT”

whereas the Salesforce format has a “T” (between the date and time) and a “Z” (after the time) in there for good measure.


I spent a while trying to do an R conversion, then after some minor irritation just did my own hack function which appears to work:


Now, if you do the code in full as below, you can query using RForcecom and only select records within certain time periods!

# Assume already logged in to the API... 
# run the function
# get the time you want to query
# convert it so Salesforce is happy
# query Salesforce
q <- paste0("select Id from opportunity where LastModifiedDate > ",time_now)

I’m sure there’s a real way around this but I couldn’t find it on Google, so this may prove helpful to someone.

Also, I believe the T and Z are something to do with time zone, so please take extra care when working across multiple timezones.

To leave a comment for the author, please follow the link and comment on their blog: Dan Thompson. 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.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


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)