Reputation: 143
I'm using simple_salesforce to get a list of all field names for an object.
I have the following
from simple_salesforce import Salesforce
sf = Salesforce(username = username, password = pass....) #Connecting to sf
#I'm using salesforce object api names to get all the fields that belong to that object
Api_names = ["Account", "Contact", "Campaign"]
desc = {}
for obj in Api_names:
if obj == "Account":
desc = sf.Account.describe()
elif obj == "Contact":
desc = sf.Contact.describe()
elif obj == "Campaign":
desc = sf.Campaign.describe()
field_names_list_1 = [field['name'] for field in desc['fields']]
#do something with the list... for now, I'm just adding break
break
I want to write something like this
Api_names = ["Account", "Contact", "Campaign"]
for obj in Api_names:
desc = sf.obj.describe()
But it's treating obj as an api name. I did try the soql way
query = sf.query("SELECT QualifiedApiName FROM FieldDefinition WHERE
EntityDefinition.QualifiedApiName IN ('{0}')".format("Account"))
field_names_list_2 = []
for i in range(0, query['totalSize']):
field_names_list_2.append(query['records'][i]["QualifiedApiName"])
But, for Account object, field_names_list_1
and field_names_list_2
have different lengths. field_names_list_1 contains all the fields but field_names_list_2 is missing some fields. Not sure why.
What can I change in sf.obj.describe()
so it's treats obj as an actual variable instead of a salesforce object name?
Upvotes: 4
Views: 3893
Reputation: 2759
Use getattr()
to dynamically acquire an object proxy in simple_salesforce
:
api_names = ["Account", "Contact", "Campaign"]
for obj in Api_names:
desc = getattr(sf, obj).describe()
Upvotes: 3