1

I am trying to pass a variable into SOQL statement, I am using simple_salesforce library. I only need to grab the data when CreateDate of the Opportunity is > than yesterday, yesterday's date is saved in last_run_day variable. Below is my code:

yesterday = date.today() + timedelta(days=-1)
last_run_date = yesterday.strftime("%Y-%m-%d"+"T"+"%H:%M:%S"+"Z")

sf_data = sf.query_all("SELECT ID, Name FROM Opportunity where probability > 0 AND CreatedDate > '%s' AND AdPoint_Id__c <> NULL ORDER BY AdPoint_Id__c" % last_run_date)
print(sf_data)

Gives me an error: field 'CreatedDate' must be of type dateTime and should not be enclosed in quotes", 'errorCode': 'INVALID_FIELD'

4
  • 1
    You can either remove the quotes surrounding %s otherwise the date will be considered as String, which is not correct OR simply write where CreatedDate > YESTERDAY. See date literals here developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/… – manjit5190 Apr 4 '20 at 19:55
  • Perfect, that actually worked and there was no need to construct a variable last_run_date, Thank you very much! – Chique_Code Apr 4 '20 at 20:06
  • Great! Added it as an answer. – manjit5190 Apr 4 '20 at 20:07
  • Side note - fstrings make your strings more readable in many cases than old-style % formatting. – David Reed Apr 4 '20 at 20:20
1

You can either remove the quotes surrounding %s otherwise the date will be considered as String, which is not correct OR simply write where CreatedDate > YESTERDAY.

Please see date literals here.o https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.