[SalesForce] Invalid Date Format for CloseDate with simple salesforce

I'm encountering some issues when attempting to create an opportunity using the python library simple salesforce.

I've verified that CloseDate is a Date Type field.

enter image description here

With the simple salesforce wrapper, I can connect to my account, create Account records, and query Opportunities. However, when I try to run the following

sf.Opportunity.create({'CloseDate' : "2018-05-09", 'Amount' : 20.0, 'StageName' : 'Closed Won', 'Name' : 'FIRST LAST'})

I get the following traceback.

SalesforceMalformedRequest: Malformed request https://cs47.salesforce.com/services/data/v38.0/sobjects/Opportunity/. Response content: [{'message': 'Invalid Date', 'errorCode': 'UNKNOWN_EXCEPTION', 'fields': []}]

Those 4 fields are the only required ones with CloseDate being the only Date. I have tried all sorts of variations on the format of the date without any luck (slashes instead of hyphens, datetime objects, etc). This includes adding time on to the end of the date just in case it was looking for that format. Tried a bunch of different formats from the dev forums as well as from the docs. Any help would be greatly appreciated. Thanks.

Best Answer

I spun up a Salesforce DX scratch org and could not reproduce this issue, copying and pasting the exact line from the question.

$ python3
>>> from simple_salesforce import Salesforce 
>>> sf = Salesforce(session_id=SFDX_SESSION_ID, instance_url=SCRATCH_URL)
>>> sf.query('SELECT Id FROM Opportunity')
OrderedDict([('totalSize', 0), ('done', True), ('records', [])])
>>> sf.Opportunity.create({'CloseDate' : "2018-05-09", 'Amount' : 20.0, 'StageName' : 'Closed Won', 'Name' : 'FIRST LAST'})
OrderedDict([('id', '0061F000002v5DrQAI'), ('success', True), ('errors', [])])
>>> sf.query('SELECT CloseDate FROM Opportunity')
OrderedDict([('totalSize', 1), ('done', True), ('records', [OrderedDict([('attributes', OrderedDict([('type', 'Opportunity'), ('url', '/services/data/v38.0/sobjects/Opportunity/0061F000002v5DrQAI')])), ('CloseDate', '2018-05-09')])])])

Given that you are also observing the issue in Workbench, I wonder if you have some code running on Opportunity insert that somehow generates this error as a confusing side-effect. However, based on the current simple_salesforce source tree, this particular response is only generated on an actual 400 error code from the server.

The date format, at any rate, looks like it is probably not the true issue. I have an internal ETL tool written in Python with simple_salesforce that inserts thousands of records with dates in this format, and those work.

The place I get errors with simple_salesforce and Date fields is an empty string ('') read from a CSV in a Date column. This yields

Cannot deserialize instance of date from VALUE_STRING value or request may be missing a required field

as do other obvious failures in date formatting. That suggests to me, again, that the source of this issue is unique to your org in some fashion.

Do you have automation in place on Opportunity that could in any way manipulate a date field (even one other than Close Date)? Is your org in a non-United States locale? Is there any possibility that the data you're copying and pasting on your local machine includes control or nonprinting characters?

Related Topic