Simple_Salesforce: making bulk SQL calls within a date range
Asked Answered
S

1

2

I'm using Simple_Salesforce to grab a chunk of data using the salesforce api. I was wondering if there was anyway to specify a date range when making calls. I keep getting the following error.

query = 'SELECT Id, Name FROM Account WHERE createddate > 1451621381000'

sf.bulk.Account.query(query)

  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/simple_salesforce/bulk.py", line 157, in _get_batch_results
    url_query_results = "{}{}{}".format(url, '/', result.json()[0])
IndexError: list index out of range

query = 'SELECT Id, Name FROM Account WHERE createddate > 2017-01-01'

This works, so I can filter on conditions

query = "SELECT Id, CreatedDate FROM Tbl WHERE Id = '500G0000008LeHzIAK'"
dd = sf.bulk.Tbl.query(query)
df = pd.DataFrame(dd)

However, date seems to be saved in an odd manner and this throws an error

query = "SELECT Id, CreatedDate FROM Case Tbl CreatedDate = '1328828872000L'"
query = "SELECT Id, CreatedDate FROM Case Tbl CreatedDate > '1328828872000L'"
dd = sf.bulk.Tbl.query(query)
df = pd.DataFrame(dd)

Date values look like this: 1463621383000L

Spector answered 21/7, 2017 at 15:23 Comment(0)
D
4

Datetime fields (e.g. CreatedDate) can be filtered only by values in Datetime format, with or without timezone offset, all without quotes. (read more about SOQL Date Formats)

SELECT Id FROM Case WHERE CreatedDate > 2017-01-31T23:59:59Z

Date fields can be filtered only by values in Date format

SELECT Id FROM Opportunity WHERE CloseDate > 2017-01-31

If you are interested in records created e.g. in the last minute, you can build the value by:

sf_timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(time.time() - 60))
sql = "SELECT Id FROM Account WHERE CreatedDate > %s" % sf_timestamp

I see that your numeric timestamp is in miliseconds, therefore you must divide it by 1000 first.

I don't believe that your example createddate > 2017-01-01 works for you because it shouldn't work according the reference. You get an error message:

Error: value of filter criterion for field 'createddate' must be of type dateTime and should not be enclosed in quotes

It seems that Simple Salesforce doesn't report correctly the original error message from Salesforce and only subsequent errors are eventually reported by Simple-Salesforce. (All examples verified, but not by simple-salesforce.)

Denunciatory answered 22/7, 2017 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.