Python 简单 Salesforce Select 所有字段
Python Simple Salesforce Select All Fields
我正在使用 Python Simple-Salesforce 通过 SOQL 查询数据。我知道 SOQL 语法不支持 "SELECT *",所以我想创建一个 Python 脚本来收集要插入到 SELECT 语句中的所有字段的字符串列表。以下是我描述帐户对象的方式:
from simple_salesforce import Salesforce
from simple_salesforce import SFType
#(credentials hidden)
sf = Salesforce(username=username, password=password,
security_token=security_token, sandbox=True,
client_id='mwheeler App')
desc = sf.Account.describe()
print(desc)
我应该如何从如下所示的有序字典中将字段名称提取到字符串列表中?
desc:
OrderedDict([('actionOverrides', []), ('activateable', 假), ('childRelationships', [OrderedDict([('cascadeDelete', 假), ('childSObject', 'Account'), ('deprecatedAndHidden', 假), ('field', 'ParentId'), ('junctionIdListNames', []), ( 'junctionReferenceTo', []), ('relationshipName', 'ChildAccounts'), ('restrictedDelete', 假)]), OrderedDict([('cascadeDelete', 真), ( 'childSObject', 'AccountCleanInfo'), ('deprecatedAndHidden', 假), ('field', 'AccountId'), ......
我将使用字符串列表 select 所有字段:
query = sf.query_all("SELECT string_list FROM Account")
这个python库描述调用可以在这里看到:
如果我是你,我会追溯他们最初是如何获得有序词典的。
从这一行可以看出:
他们使用这里的 Base URL:
您可以在 Workbench:
中进行相同的调用
https://workbench.developerforce.com/login.php
通过简单的 google 搜索,您可以找到一些关于如何遍历字典的有用示例,这里有一些:
How to do this - python dictionary traverse and search
Loop through all nested dictionary values?
Walking/iterating over a nested dictionary of arbitrary depth (the dictionary represents a directory tree)
只要您知道要查找的内容,遍历字典应该很容易。
警告,根据我查询所有字段的经验,对于像 FFLib 这样的企业框架非常有用,但是有些对象并未设计为在一个 SOQL 查询中包含所有字段。
请参阅此页面了解 SOQL 限制:
希望这对您有所帮助。
How should I extract the field names into a string list from the Ordered Dictionary shown below?
我已经扩展了您的代码以包含解决方案
from simple_salesforce import Salesforce
#(credentials hidden)
sf = Salesforce(username=username, password=password,
security_token=security_token, sandbox=True,
client_id='mwheeler App')
desc = sf.Account.describe()
# Below is what you need
field_names = [field['name'] for field in desc['fields']]
soql = "SELECT {} FROM Account".format(','.join(field_names))
results = sf.query_all(soql)
# Alternative method to retrieve results
# I don't have any recommendation which to use
results = sf.bulk.Account.query(soql)
我知道这个问题是前段时间发的,只是希望它有一个完整的解决方案。
# provide credential information for Salesforce Session Object
username = 'username'
password = 'password'
security_token = 'security_token'
domain = 'login'
# Create salesforce session
sf = Salesforce(username=username,
password=password,
security_token=security_token,
domain=domain)
# Get list of fields for TABLE
fields = [field.get('name') for field in getattr(sf, TABLE).describe().get('fields')]
# Concat fields ready for inclusion in query string
fields = ',\n'.join(fields)
# Convert into SOQL query
soql = f"SELECT \n{fields} \nFROM {TABLE}"
我正在使用 Python Simple-Salesforce 通过 SOQL 查询数据。我知道 SOQL 语法不支持 "SELECT *",所以我想创建一个 Python 脚本来收集要插入到 SELECT 语句中的所有字段的字符串列表。以下是我描述帐户对象的方式:
from simple_salesforce import Salesforce
from simple_salesforce import SFType
#(credentials hidden)
sf = Salesforce(username=username, password=password,
security_token=security_token, sandbox=True,
client_id='mwheeler App')
desc = sf.Account.describe()
print(desc)
我应该如何从如下所示的有序字典中将字段名称提取到字符串列表中?
desc:
OrderedDict([('actionOverrides', []), ('activateable', 假), ('childRelationships', [OrderedDict([('cascadeDelete', 假), ('childSObject', 'Account'), ('deprecatedAndHidden', 假), ('field', 'ParentId'), ('junctionIdListNames', []), ( 'junctionReferenceTo', []), ('relationshipName', 'ChildAccounts'), ('restrictedDelete', 假)]), OrderedDict([('cascadeDelete', 真), ( 'childSObject', 'AccountCleanInfo'), ('deprecatedAndHidden', 假), ('field', 'AccountId'), ......
我将使用字符串列表 select 所有字段:
query = sf.query_all("SELECT string_list FROM Account")
这个python库描述调用可以在这里看到:
如果我是你,我会追溯他们最初是如何获得有序词典的。
从这一行可以看出:
他们使用这里的 Base URL:
您可以在 Workbench:
中进行相同的调用https://workbench.developerforce.com/login.php
通过简单的 google 搜索,您可以找到一些关于如何遍历字典的有用示例,这里有一些:
How to do this - python dictionary traverse and search
Loop through all nested dictionary values?
Walking/iterating over a nested dictionary of arbitrary depth (the dictionary represents a directory tree)
只要您知道要查找的内容,遍历字典应该很容易。
警告,根据我查询所有字段的经验,对于像 FFLib 这样的企业框架非常有用,但是有些对象并未设计为在一个 SOQL 查询中包含所有字段。
请参阅此页面了解 SOQL 限制:
希望这对您有所帮助。
How should I extract the field names into a string list from the Ordered Dictionary shown below?
我已经扩展了您的代码以包含解决方案
from simple_salesforce import Salesforce
#(credentials hidden)
sf = Salesforce(username=username, password=password,
security_token=security_token, sandbox=True,
client_id='mwheeler App')
desc = sf.Account.describe()
# Below is what you need
field_names = [field['name'] for field in desc['fields']]
soql = "SELECT {} FROM Account".format(','.join(field_names))
results = sf.query_all(soql)
# Alternative method to retrieve results
# I don't have any recommendation which to use
results = sf.bulk.Account.query(soql)
我知道这个问题是前段时间发的,只是希望它有一个完整的解决方案。
# provide credential information for Salesforce Session Object
username = 'username'
password = 'password'
security_token = 'security_token'
domain = 'login'
# Create salesforce session
sf = Salesforce(username=username,
password=password,
security_token=security_token,
domain=domain)
# Get list of fields for TABLE
fields = [field.get('name') for field in getattr(sf, TABLE).describe().get('fields')]
# Concat fields ready for inclusion in query string
fields = ',\n'.join(fields)
# Convert into SOQL query
soql = f"SELECT \n{fields} \nFROM {TABLE}"