简单的 Salesforce 日期上传

问题描述

我正在尝试使用 openpyxl 和 simplesfoce 将机会上传到 Salesforce。以下是我为使上传工作而进行的当前尝试。我已经试过了,一个普通的 excel 日期对象,excel 日期对象作为字符串,excel 日期对象作为一个字符串,时间部分被切断。

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 对象收到一个错误,说明它们不能被 JSON 序列化,所有其他对象都会收到以下错误。下面的信息是尝试发送到 Salesforce 的信息,错误是发回的错误消息。

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'}

我已经获取了这些确切的数据并手动创造了一个很好的机会,所以我看到错误的唯一原因是日期的格式。

解决方法

正确的日期格式如eyescream 提到的“2021-06-02”。这里的错误只是指这些项目之一中的错误自定义字段。如果您遇到此类错误,请尝试删除不需要的字段以找出导致错误的字段。