如何通过simple_salesforce进行Salesforce Bulk API调用?

fly*_*zai 2 python salesforce simple-salesforce

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

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

Ter*_*nus 10

代码确实有一些评论.还有这个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)
Run Code Online (Sandbox Code Playgroud)

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

<your Salesforce object>.bulk.<Name of the Object>.<operation to perform>(<appropriate parameter, based on your operation>)
Run Code Online (Sandbox Code Playgroud)
  • <your Salesforce object> 是你从构建中获得的对象 simple_salesforce.Salesforce(<credentials>)
    • <credentials>是你的username,password,security_token,和sandbox(布尔,如果你连接到一个沙箱)或session_id.(这是我所知道的两种方式)
  • <Name of the Object>仅仅是Account或Opportunity或任何物体你试图操纵
  • <operation to perform> 是以下之一:
    • 询问
    • 插入
    • 更新
    • UPSERT
    • hard_delete(我的帐户没有相应的权限来测试此操作.任何提及都是纯粹的推测)
  • <appropriate parameter> 取决于您希望执行的操作
    • query - 包含SOQL的字符串
    • insert - 字典列表.记住在创建新记录时,为组织所需的所有字段设置密钥
    • 更新 - 词典列表.你显然需要每个字典有效的Object Id
    • upsert - 字典列表和表示"外部id"列的字符串."外部ID"可以是Salesforce对象'Id'或任何其他列; 做出明智的选择.如果任何字典没有与"外部ID"相同的密钥,则将创建新记录.
  • 返回的内容:取决于操作.
    • 查询返回包含结果的字典列表.除了查询列之外,每个字典都有一个'attributes'键.这包含一个'url'键,看起来它可以用于特定对象,键和'type'键的api请求,这是返回的Object的类型
    • insert/update/upsert返回字典列表.每个字典都像{'errors': [], 'success': True, 'created': False, 'id': 'id of object would be here'}

感谢@ ATMA关于如何使用的问题query.随着这个问题的源代码,能够弄清楚insert,update和upsert.