首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过simple_salesforce进行Salesforce批量API调用?

如何通过simple_salesforce进行Salesforce批量API调用?
EN

Stack Overflow用户
提问于 2017-04-07 20:21:40
回答 2查看 10.2K关注 0票数 6

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-26 23:34:18

密码确实有一些评论。还有这个读文档页,但是,即使这样,它也需要一些帮助。

好东西先,下面解释一下。

代码示例(假设您同时运行整个代码块):

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>是您的usernamepasswordsecurity_tokensandbox(如果您正在连接到沙箱)或session_id。(这是我所知道的两种方式)

  • <Name of the Object>只是Account或者Opportunity或者你想要操纵的任何东西
  • <operation to perform>是以下内容之一:
    • 查询
    • 插入
    • 更新
    • 上插
    • hard_delete (我的帐户没有测试此操作的适当权限。任何提及都是纯粹的猜测)

  • <appropriate parameter>取决于您希望执行的操作。
    • 查询-包含SOQL的字符串
    • 插入-字典列表。创建新记录时,请记住为您的组织所需的所有字段设置一个键。
    • 更新-一份字典列表。显然,每个字典都需要一个有效的对象Id
    • upsert --一个字典列表和一个表示“外部id”列的字符串。“外部id”可以是Salesforce对象'Id'或任何其他列;请明智地选择。如果任何字典没有与“外部id”相同的键,则将创建一个新记录。

  • 返回的内容:取决于操作。
    • 查询返回包含结果的字典列表。除了查询的列外,每个字典都有一个'attributes'键。它包含一个'url'键,看起来它可以用于特定对象的api请求、键和'type'键,这是返回的对象的类型。
    • insert/update/upsert返回字典列表。每个字典都像{'errors': [], 'success': True, 'created': False, 'id': 'id of object would be here'}

感谢@ATMA的问题展示了如何使用query。通过这个问题和源代码,能够找到insertupdateupsert

票数 20
EN

Stack Overflow用户

发布于 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行时,您需要更加小心,这样才能使顺序在头和数据行之间匹配。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43286524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档