使用简单的 salesforce 循环获取每个 salesforce 对象的字段名称
Using simple salesforce to get field names for each salesforce object in a loop
我正在使用 simple_salesforce 获取一个对象的所有字段名称的列表。
我有以下
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
我想写这样的东西
Api_names = ["Account", "Contact", "Campaign"]
for obj in Api_names:
desc = sf.obj.describe()
但它将 obj 视为 api 名称。我确实尝试了 soql 方式
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"])
但是,对于 Account 对象,field_names_list_1
和 field_names_list_2
具有不同的长度。 field_names_list_1 包含所有字段,但 field_names_list_2 缺少一些字段。不知道为什么。
我可以在 sf.obj.describe()
中更改什么,以便将 obj 视为实际变量而不是 salesforce 对象名称?
使用getattr()
动态获取simple_salesforce
中的对象代理:
api_names = ["Account", "Contact", "Campaign"]
for obj in Api_names:
desc = getattr(sf, obj).describe()
我正在使用 simple_salesforce 获取一个对象的所有字段名称的列表。
我有以下
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
我想写这样的东西
Api_names = ["Account", "Contact", "Campaign"]
for obj in Api_names:
desc = sf.obj.describe()
但它将 obj 视为 api 名称。我确实尝试了 soql 方式
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"])
但是,对于 Account 对象,field_names_list_1
和 field_names_list_2
具有不同的长度。 field_names_list_1 包含所有字段,但 field_names_list_2 缺少一些字段。不知道为什么。
我可以在 sf.obj.describe()
中更改什么,以便将 obj 视为实际变量而不是 salesforce 对象名称?
使用getattr()
动态获取simple_salesforce
中的对象代理:
api_names = ["Account", "Contact", "Campaign"]
for obj in Api_names:
desc = getattr(sf, obj).describe()