是否可以获取对象上所有字段的所有 API 名称列表?
Schema.DescribeSObjectResult r =Object__c.sObjectType.getDescribe();
List<String>apiNames = new list<String>();
for(Schema.DescribeSObjectResult result : r){
apiNames.add(); //this is where I am lost.
}
您可以使用 fields 方法来获取 Schema.SObjectTypeFields。
Schema.DescribeSObjectResult r = Account.sObjectType.getDescribe();
List<String>apiNames = new list<String>();
for(string apiName : r.fields.getMap().keySet()){
apiNames.add(apiName);
}
System.debug(apiNames);
也有一个通用的解决方案
`String SobjectApiName = 'Account';
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
List<Schema.SObjectField> lstFieldApiNames = fieldMap.values();
system.debug('fieldName->>'+lstFieldApiNames);`