0

I am trying to query sfdc using a datetime, I tried using the date as a string, then as a datetime object, but I get malformed query for using it like this

dateTime = sys.argv[1] 

result = sf.query("select Case__r.CaseNumber from File_Attachment__c where ( LastModifiedDate >= dateTime) ")

I also tried

from dateutil.parser import parse dtime = parse(dateTime)

result = sf.query("select Case__r.CaseNumber from File_Attachment__c where ( LastModifiedDate >= dtime) ")

and

result = sf.query("select Case__r.CaseNumber from File_Attachment__c where ( LastModifiedDate >= :dtime) ")

but all give me malformed query error from sfdc. Can anyone help?

Hleb
  • 7,037
  • 12
  • 58
  • 117
nkshirsa
  • 11
  • 3
  • how are you formatting your date? you SOQL for the LastmodifiedDate should look something like this: where LastModifiedDate > 2005-05-18T14:01:00-04:00 – glls May 19 '16 at 05:23

2 Answers2

0

using beatbox and python 2.7 the following code executes a successful query. Either you are using a different python version, the date format is incorrect or the query parameters are incorrect (Case__r.CaseNumber Or File_Attachment__c)

import beatbox

"salesforceusername and password"
username = 'xxx'
password = "xxx"
token    = 'xxx'

"""conenct and authenticate"""
svc = beatbox.PythonClient()
svc.login(username, password+token)

"""execut SOQL query"""
res = svc.query("select ID from Case where LastModifiedDate >= 2005-05-18T14:01:00-04:00")

"""prints results in console"""
print(res)
glls
  • 2,325
  • 1
  • 22
  • 39
0

SFDC link for Datetime - https://help.salesforce.com/articleView?id=000325035&type=1&mode=1 Python link for Datetime - https://www.journaldev.com/23365/python-string-to-datetime-strptime#:~:text=Python%20strptime(),-Python%20strptime()&text=This%20function%20is%20exactly%20opposite,datetime%20object%20to%20a%20string.&text=Here%20the%20function%20returns%20struct_time,returned%20by%20ctime()%20function

from simple_salesforce import Salesforce
sf = Salesforce(
username='XXX', 
password='XXX', 
security_token='XXX')
result = sf.query("SELECT Id, Email FROM Contact WHERE LastName = 'Jones'")
print(result)

click the link to see the python code Image and its Result