我正在尝试执行以下代码,以使用 simple_salesforce
python 库将数据推送到 Salesforce:
from simple_salesforce import Salesforce
staging_df = hive.execute("select * from hdmni")
staging_df = staging_df.toPandas()
# # staging_df['birth_date']= staging_df['birth_date'].dt.date
staging_df['birth_date'] = staging_df['birth_date'].astype(str)
staging_df['encounter_start_date'] = staging_df['encounter_start_date'].astype(str)
staging_df['encounter_end_date'] = staging_df['encounter_end_date'].astype(str)
bulk_data = []
for row in staging_df.itertuples():
d= row._asdict()
del d['Index']
bulk_data.append(d)
sf = Salesforce(password='', username='', security_token='')
sf.bulk.Delivery_Detail__c.insert(bulk_data)
我在尝试将字典发送到 salesforce 时收到此错误:
SalesforceMalformedRequest: Malformed request https://subhotutorial-dev-ed.my.salesforce.com/services/async/38.0/job/7500o00000HtWP6AAN/batch/7510o00000Q15TnAAJ/result. Response content: {'exceptionCode': 'InvalidBatch', 'exceptionMessage': 'Records not processed'}
最佳答案
您的查询有些内容不正确。虽然我不知道您的用例,但通过阅读这一行,您可以知道您正在尝试插入 Salesforce 中的自定义对象/实体:
sf.bulk.Delivery_Detail__c.insert(bulk_data)
您可以看出的原因是因为 __c
后缀,它被附加到自定义对象和字段上(顺便说一下,这是两个下划线)。
由于您要插入自定义对象,因此您的字段也必须是自定义的。请注意,您还没有将该后缀附加到它们上。
Note: Every custom object/entity in Salesforce does come with a few standard fields to support system features like record key (
Id
), record name (Name
), audit fields (CreatedById
,CreatedDate
, etc.). These wouldn't have a suffix. But none of the fields you reference are any of these standard system fields...so the__c
suffix would be expected.
我怀疑 Salesforce 在您的插入操作中期望的是这样的字段名称:
Birth_Date__c
Encounter_Start_Date__c
Encounter_End_Date__c
这些被称为对象和字段的 API 名称,任何时候代码与它们交互(无论是通过集成,还是在直接在 Salesforce 平台上执行的代码上),您都需要确定您正在使用此 API 名称。
顺便说一句,您可以通过多种方式检索此 API 名称。最简单的方法可能是登录您的 Salesforce 组织,然后在设置 > 对象管理器 > [某些对象] > 字段和关系中,您可以查看每个字段的详细信息,包括 API 名称。这是屏幕截图。
您还可以使用 SObject 描述 API,无论是在 native Apex 代码中,还是通过集成和 REST或SOAP蜜蜂。以下是从描述 API 请求到描述 REST 端点的响应的一部分,该对象与上面的 UI 示例相同,可在 https://[domain]/services/data/v47.0/sobjects/Expense__c 找到/描述
:
查看您正在使用的 simple-salesforce python 库的文档,他们已经公开了描述 API。你可以找到一些info在“其他选项”下。您可以将其调用为sf.SObject.describe,其中“SObject”是您想要查找其信息的实际对象。例如,在您的情况下,您将使用:
sf.Delivery_Detail__c.describe()
作为与 Salesforce 对象交互时的第一个故障排除步骤,我始终建议仔细检查是否正确引用 API 名称。我无法告诉你有多少次我遇到了诸如添加或缺少下划线之类的小事情。特别是带有 __c
后缀。
关于python - 获取 SalesforceMalformedRequest : Malformed request error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59100656/