4

我正在使用该模块simple-salesforce,并且在文档中没有看到有关进行批量 API 调用的任何内容。有人知道怎么做吗?

https://github.com/simple-salesforce/simple-salesforce

4

2 回答 2

16

代码确实有一些注释。还有这个 readthedocs 页面,但即使这样看起来也需要一些帮助。

先说好东西,下面解释。

代码示例(假设您一次运行整个代码块):

from simple_salesforce import Salesforce

sf = Salesforce(<credentials>)

# query
accounts = sf.bulk.Account.query('SELECT Id, Name FROM Account LIMIT 5')
# returns a list of dictionaries similar to: [{'Name': 'Something totally new!!!', 'attributes': {'url': '/services/data/v38.0/sobjects/Account/object_id_1', 'type': 'Account'}, 'Id': 'object_id_1'}]

# assuming you've pulled data, modify it to use in the next statement
accounts[0]['Name'] = accounts[0]['Name'] + ' - Edited'
# update
result = sf.bulk.Account.update(accounts)
# result would look like [{'errors': [], 'success': True, 'created': False, 'id': 'object_id_1'}]

# insert
new_accounts = [{'Name': 'New Bulk Account - 1', 'BillingState': 'GA'}]
new_accounts = sf.bulk.Account.insert(new_accounts)
# new_accounts would look like [{'errors': [], 'success': True, 'created': True, 'id': 'object_id_2'}]

# upsert
accounts[0]['Name'] = accounts[0]['Name'].replace(' - Edited')
accounts.append({'Name': 'Bulk Test Account'})
# 'Id' is the column to "join" on. this uses the object's id column
upserted_accounts = sf.bulk.Account.upsert(accounts, 'Id')
# upserted_accounts would look like [{'errors': [], 'success': True, 'created': False, 'id': 'object_id_1'}, {'errors': [], 'success': True, 'created': True, 'id': 'object_id_3'}]

# how i assume hard_delete would work (i never managed to run hard_delete due to insufficient permissions in my org)
# get last element from the response.
# *NOTE* This ASSUMES the last element in the results of the upsert is the new Account.
#  This is a naive assumption
new_accounts.append(upserted_accounts[-1])
sf.bulk.Account.hard_delete(new_accounts)

使用 simple_salesforce,您可以通过执行访问批量 api

<your Salesforce object>.bulk.<Name of the Object>.<operation to perform>(<appropriate parameter, based on your operation>)
  • <your Salesforce object>是你从构造中得到的对象simple_salesforce.Salesforce(<credentials>)
    • <credentials>是你的username, password, security_token, and sandbox(bool, if you're connected to a sandbox) or session_id. (这是我知道的两种方式)
  • <Name of the Object>只是AccountOpportunity您试图操纵的任何对象
  • <operation to perform>是以下之一:
    • 询问
    • 插入
    • 更新
    • 上插
    • hard_delete(我的帐户没有适当的权限来测试此操作。任何提及纯属猜测)
  • <appropriate parameter>取决于您希望执行的操作
    • 查询 - 包含 SOQL 的字符串
    • 插入 - 字典列表。请记住在创建新记录时为您的组织所需的所有字段设置一个键
    • 更新 - 字典列表。您显然需要每个字典的有效对象 ID
    • upsert - 字典列表和表示“外部 id”列的字符串。“外部 id”可以是 Salesforce 对象'Id'或任何其他列;做出明智的选择。如果任何字典没有与“external id”相同的键,则会创建一条新记录。
  • 返回的内容:取决于操作。
    • 查询返回带有结果的字典列表。除了查询的列之外,每个字典都有一个'attributes'键。这里面包含了一个'url'key,貌似可以用于api请求具体的object,key和'type'key,也就是返回的Object的类型
    • insert/update/upsert 返回一个字典列表。每个字典就像{'errors': [], 'success': True, 'created': False, 'id': 'id of object would be here'}

感谢@ATMA 的问题展示了如何使用query. 有了那个问题和源代码,就能够弄清楚insert,updateupsert.

于 2017-10-26T23:34:18.303 回答
0

几周前我遇到了同样的问题。可悲的是,没有办法使用 simple-salesforce 来做到这一点。我通过源代码进行的研究似乎没有任何方法可以做到这一点或破解它以使其工作。

我研究了许多其他基于 Python 的批量 API 工具。其中包括 Salesforce-bulk 1.0.7 ( https://pypi.python.org/pypi/salesforce-bulk/1.0.7 )、Salesforce-bulkipy 1.0 ( https://pypi.python.org/pypi/salesforce-bulkipy ) 和 Salesforce_bulk_api ( https://github.com/safarijv/salesforce-bulk-api )。

我在系统上配置 Salesforce-bulk 1.0.7 和 Salesforce-bulkipy 1.0 时遇到了一些问题,但 Salesforce_bulk_api 运行良好。它使用 simple-salesforce 作为身份验证机制,但会为您处理批量作业的创建和上传记录。

需要注意的是,simple-salesforce 和批量 API 的工作方式不同。Simple-Salesforce 通过 REST 工作,因此您只创建 JSON 字符串 - 这很容易与 Python dicts 兼容。批量 API 使用上传到 Salesforce 的 CSV 文件。创建这些 CSV 可能有点危险,因为标题中字段名称的顺序必须与文件中数据元素的顺序相对应。这不是什么大不了的事,但是在创建标题和数据行之间的顺序匹配的 CSV 行时,您需要更加小心。

于 2017-05-17T13:25:51.103 回答