我使用的是模块simple-salesforce,在文档中没有看到任何关于进行批量API调用的内容。有人知道怎么做吗?
发布于 2017-10-26 23:34:18
密码确实有一些评论。还有这个读文档页,但是,即使这样,它也需要一些帮助。
好东西先,下面解释一下。
代码示例(假设您同时运行整个代码块):
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和sandbox(如果您正在连接到沙箱)或session_id。(这是我所知道的两种方式)
<Name of the Object>只是Account或者Opportunity或者你想要操纵的任何东西<operation to perform>是以下内容之一:
<appropriate parameter>取决于您希望执行的操作。'Id'或任何其他列;请明智地选择。如果任何字典没有与“外部id”相同的键,则将创建一个新记录。
'attributes'键。它包含一个'url'键,看起来它可以用于特定对象的api请求、键和'type'键,这是返回的对象的类型。{'errors': [], 'success': True, 'created': False, 'id': 'id of object would be here'}
感谢@ATMA的问题展示了如何使用query。通过这个问题和源代码,能够找到insert、update和upsert。
发布于 2017-05-17 13:25:51
几周前我遇到了同样的问题。可悲的是,没有办法用简单的销售力量来做这件事。我通过资料来源进行的研究似乎没有任何方法可以做到这一点,也没有办法破解它来使它发挥作用。
我研究了许多其他基于Python的批量API工具。其中包括Salesforce-散装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-散装1.0.7和Salesforce-bulkipy1.0,但是Salesforce_bulk_api运行得很好。它使用simple-salesforce作为身份验证机制,但是处理批量作业的创建和为您上传记录。
提醒一下,简单的salesforce和批量API的工作方式是不同的。简单-Salesforce通过REST工作,因此您只能创建JSON字符串,这些字符串很容易与Python兼容。批量API与上传到Salesforce的CSV文件一起工作。创建那些CSV可能有点危险,因为标题中字段名的顺序必须与文件中数据元素的顺序相对应。这不是什么大问题,但是在创建您的CSV行时,您需要更加小心,这样才能使顺序在头和数据行之间匹配。
https://stackoverflow.com/questions/43286524
复制相似问题