Simple Salesforce Date upload

946 Views Asked by At

I am trying to upload an opportunity to Salesforce using openpyxl and simplesfoce. Below is the current attempt I have made to get the upload to work. I have tried that, a normal excel date object, excel date object as a string, and excel date object as a string with the time portion cut off.

ws = wb["Opportunities"] # Gets the Opportunities sheet
recordTypeMap = getRecordTypes(sf, ws, 1, "Opportunity")
insertOpportunities = []
try:
    logInfo("Reading Opportunities")
    for row in ws.iter_rows(min_row=2, values_only=True):
        insertOpportunities.append(
            {'RecordTypeId': recordTypeMap.get(row[0]), 'OwnerId': users.get(row[1]), 'AccountId': accounts.get(row[2]),
            'Name': row[3], 'Type': row[4], 'Budget_Confirmed__c': row[5], 'Discovery_Completed__c': row[6],
            'ROI_Analysis_Completed__c': row[7], 'EEP_Loss_Reason__c': row[8], 'CloseDate': str(datetime.datetime.now(pytz.UTC)), 'StageName': row[10],
            'LeadSource': row[13], 'EEP_Producer_CBU__c': row[14],
            'EEP_Producer_Distribution_Channel__c': row[15], 'EEP_Restricted_Access__c': row[16]})
        logging.info(insertOpportunities)
except Exception as ex:
    logError("Could not read Opportunities", ex)

try:
    logInfo("Creating Opportunities")
    Opportunities = sf.bulk.Lead.insert(insertOpportunities, batch_size=100)
    logInfo("Created Opportunities")
    logging.info(Opportunities)
except Exception as ex:
    logError("Could not create Opportunities", ex)

Date objects get an error that they cannot be JSON seralized and all others get the below error. The info below is what is trying to be sent to Salesforce and the error is the error message sent back.

INFO: [{'RecordTypeId': '0124W000001lDpAQAU', 'OwnerId': '0056t000000ENvxAAG', 'AccountId': '0016t000002tw3UAAQ', 'Name': 'Test op 1', 'Type': 'Existing Business', 'Budget_Confirmed__c': True, 'Discovery_Completed__c': True, 'ROI_Analysis_Completed__c': True, 'EEP_Loss_Reason__c': None, 'CloseDate': '2021-06-02 14:08:36.995182+00:00', 'StageName': 'Closed Won', 'LeadSource': 'Purchased List', 'EEP_Producer_CBU__c': None, 'EEP_Producer_Distribution_Channel__c': None, 'EEP_Restricted_Access__c': False}]    
ERROR: Malformed request Response content: {'exceptionCode': 'InvalidBatch', 'exceptionMessage': 'Records not processed'}

I have taken this exact data and manually made an opportunity just fine so the only reason I would see an error would be the format of the date.

1

There are 1 best solutions below

0
On BEST ANSWER

Correct date formats are as eyescream mentioned, "2021-06-02". The error here was simply referring to the wrong custom field in one of these items. If you struggle with this kind of error try removing unneeded fields to find which one is causing the error.