3

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.

  • is there any significance of the different quotes used? You use " for the date but then ' for the other strings. – battery.cord May 9 '18 at 20:49
  • The date format included in your code works as a JSON payload in the Workbench, so it may be something to do with how it serializes the data. Could you dump out what the data looks like in a log or something? – sfdcfox May 9 '18 at 21:01
  • @battery.cord no significance. I was just tinkering with how it's serializing the data. Making all of the quotes single or double still returns the same error. – justmax May 9 '18 at 21:16
  • @sfdcfox I have some records already in the Opportunity field. When I query them I get an ordered dictionary where the relevant data looks to be in the same format I'm passing through. Since the library is handling the json serialization I don't have direct access to a log, but I will see if I wrestle one out of what it's actually serializing. OrderedDict([('totalSize', 11084), ('records', OrderedDict([('type', 'Opportunity'), ('url', ('CloseDate', '2017-03-07')]), – justmax May 9 '18 at 21:22
  • @sfdcfox Based on the error, it seems to just be using json.dumps(). Which for the example outputs: '{"CloseDate": "2018-05-09", "Amount": 20.0, "StageName": "Closed Won", "Name": "VENUE NAME FIRST LAST"}' – justmax May 9 '18 at 21:27
1

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?

| improve this answer | |
  • Thanks for taking the time to spin up a scratch org. I also saw the 'Cannot deserialize instance' error pop up when I was trying out different date formats. But as you said, I think it's just rejecting obvious errors in formatting. I will investigate further tomorrow and see if there is any programs or automation running on Opportunity. Our org is in a US locale and I don't think my local machine is the issue. – justmax May 10 '18 at 2:37
  • You were right David. Turns out there were two additional fields that were required that were tied to automation on the Opportunity Object. These fields did not appear in red or get returned in any error messages because they were separate from the Opportunity itself. The first was a text field that converts and maps a time string to a date object (which is where I think the invalid date error was stemming from), and the other was a picklist to associate the Opportunity with an Account. I'll put the code that worked for me in a comment below to avoid the character limit. Thanks again! – justmax May 10 '18 at 16:48
  • sf.Opportunity.create({'Event_Date__c' : '5/10/2018 11:22 AM', 'AccountId' : '0012a00000BwdXu', 'CloseDate' : "2018-05-09", 'Amount' : 20.0, 'StageName' : 'Closed Won', 'Name' : 'FIRST LAST'}) – justmax May 10 '18 at 16:50

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.