Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Salesforce by (11.9k points)

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")

1 Answer

0 votes
by (32.1k points)
edited by

 You can extract the field names into a string list from the Ordered dictionary by using the following extension your code: 

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 hope this will solve your problem.

To learn in-depth about Workflow in Salesforce, sign up for an industry based Salesforce Course.

Related questions

Browse Categories

...