I'm using Python Simple-Salesforce to query data via SOQL. I know that "SELECT *" is not supported in SOQL syntax, so I want to create a Python script to gather a string list of all fields to insert into the SELECT statement. Below is how I am describing the Account Object:
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)
How should I extract the field names into a string list from the Ordered Dictionary shown below?
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'), ......
I will be using the string list to select all fields:
query = sf.query_all("SELECT string_list FROM Account")
How should I extract the field names into a string list from the Ordered Dictionary shown below?
I've extended your code to include the solution
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)
I realize the question was posted a while ago, just want it to have a complete solution.
This python library describe call can be seen here:
https://github.com/simple-salesforce/simple-salesforce/blob/d2ba65e977730ce987ca7d3c38e0f8965d99eec1/simple_salesforce/api.py#L184
If I was you I would track back on how they got the Ordered Dictionary in the first place.
You can see from this line:
https://github.com/simple-salesforce/simple-salesforce/blob/d2ba65e977730ce987ca7d3c38e0f8965d99eec1/simple_salesforce/api.py#L187
That they use the Base URL from here:
https://github.com/simple-salesforce/simple-salesforce/blob/d2ba65e977730ce987ca7d3c38e0f8965d99eec1/simple_salesforce/api.py#L173
Having that you can make the same call in your Workbench:
https://workbench.developerforce.com/login.php
With a simple google search you can then find some useful examples on how to traverse the dictionary, here are a few:
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)
As long as you know what you are looking for traversing a dictionary should be rather easy.
Word of warning, from my experience querying all the fields is great for Enterprise frameworks like FFLib, however some objects are not designed to have all fields in one SOQL Query.
Refer to this page for SOQL limits:
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_limits.htm
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With