Python Simple Salesforce选择所有字段

时间:2017-11-28 16:33:04

标签: python salesforce soql simple-salesforce

我正在使用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)

如何从下面显示的有序词典中将字段名称提取到字符串列表中?

递减

OrderedDict([('actionOverrides',[]),('activateable',False),('childRelationships',[OrderedDict([('cascadeDelete',False),('childSObject','Account'), ('deprecatedAndHidden',False),('field','ParentId'),('junctionIdListNames',[]),('junctionReferenceTo',[]),('relationshipName','ChildAccounts'),('restrictedDelete' ,False)]),OrderedDict([('cascadeDelete',True),('childSObject','AccountCleanInfo'),('deprecatedAndHidden',False),('field','AccountId'),.....

我将使用字符串列表选择所有字段:

query = sf.query_all("SELECT string_list FROM Account")

3 个答案:

答案 0 :(得分:7)

  

如何从下面显示的有序词典中将字段名称提取到字符串列表中?

我已扩展您的代码以包含解决方案

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)

我意识到问题是在不久前发布的,只是希望它有一个完整的解决方案。

答案 1 :(得分:1)

这个python库描述调用可以在这里看到:

https://github.com/simple-salesforce/simple-salesforce/blob/d2ba65e977730ce987ca7d3c38e0f8965d99eec1/simple_salesforce/api.py#L184

如果我是你,我会追溯他们如何获得有序词典。

你可以从这一行看到:

https://github.com/simple-salesforce/simple-salesforce/blob/d2ba65e977730ce987ca7d3c38e0f8965d99eec1/simple_salesforce/api.py#L187

他们使用此处的基本网址:

https://github.com/simple-salesforce/simple-salesforce/blob/d2ba65e977730ce987ca7d3c38e0f8965d99eec1/simple_salesforce/api.py#L173

拥有你可以在Workbench中进行相同的调用:

https://workbench.developerforce.com/login.php

通过简单的谷歌搜索,您可以找到一些有关如何遍历字典的有用示例,以下是一些:

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限制的信息,请参阅此页:

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_limits.htm

希望这有帮助。

答案 2 :(得分:0)

# 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}"